# Creating custom type of enumerations of symbols

**URL:** <https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635>\
**Category:** New to Julia\
**Tags:** structtypes\
**Created:** [December 13, 2018, 5:05pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635 "2018-12-13T17:05:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rabarar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rabarar/32/6002_2.png) [@rabarar](https://discourse.julialang.org/u/rabarar)\
**Post date:** [December 13, 2018, 5:05pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/1 "2018-12-13T17:05:21Z")

</div>

Is it possible in julia to create a named-type that is an enumeration of symbol values?

for example a type named `Mode` that constists solely of symbols `:a`, `:b`, and `c` for example.

I tried constructing it with `Val` and `Union` but failed with type conversion errors.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [December 13, 2018, 7:40pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/2 "2018-12-13T19:40:09Z")

</div>

Is this the sort of thing you are talking about?

```nohighlight
julia> struct fun{a,b,c} end

julia> fun{:a,:b,:c}()
fun{:a,:b,:c}()

julia> typeof(ans)
fun{:a,:b,:c}

julia> f(::fun{a,b,c}) where {a,b,c} = (a,b,c)
f (generic function with 1 method)

julia> f(fun{:a,:b,:c}())
(:a, :b, :c)

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [December 13, 2018, 8:01pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/3 "2018-12-13T20:01:28Z")

</div>

`@enum someenumtype A B C`  
`A`

---

<div class="post-metadata">

**Author:** ![rabarar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rabarar/32/6002_2.png) [@rabarar](https://discourse.julialang.org/u/rabarar)\
**Post date:** [December 13, 2018, 8:25pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/4 "2018-12-13T20:25:06Z")

</div>

No - I want a typed symbol that contains only specific symbols – not any symbol

---

<div class="post-metadata">

**Author:** ![rabarar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rabarar/32/6002_2.png) [@rabarar](https://discourse.julialang.org/u/rabarar)\
**Post date:** [December 13, 2018, 8:26pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/5 "2018-12-13T20:26:33Z")

</div>

A B and C aren’t symbols - I.e. :a

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [December 13, 2018, 8:36pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/6 "2018-12-13T20:36:03Z")

</div>

> [@rabarar](#):
>
> No - I want a typed symbol that contains only specific symbols – not any symbol

That is what my example in fact does. `fun{:a,:b,:c}()` is an instance of the `fun{:a,:b,:c}` type, which specifically has `:a` and `:b` and `:c` in its type, not any symbols.

```Julia
julia> typeof(fun{:a,:b,:c}()) == typeof(fun{:x,:y,:z}())
false

```

This example shows that the types are indeed considered different if the symbols change.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 13, 2018, 8:48pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/7 "2018-12-13T20:48:32Z")

</div>

Maybe:

```julia
struct ConstrainedSymbol{T}
	a::Symbol

	ConstrainedSymbol(a) = ConstrainedSymbol{Any}(a)
	ConstrainedSymbol{Any}(a) = new{Any}(a)
	ConstrainedSymbol{T}(a) where {T} = a in T ? new{T}(a) : throw("Symbol :$a is not in the supported set of symbols $T.")
end
Base.convert(::Type{Symbol}, s::ConstrainedSymbol) = s.a
Base.Symbol(s::ConstrainedSymbol) = s.a

```

```julia
julia> s = ConstrainedSymbol{(:a, :b)}(:a)
ConstrainedSymbol{(:a, :b)}(:a)

julia> s = ConstrainedSymbol{(:a, :b)}(:b)
ConstrainedSymbol{(:a, :b)}(:b)

julia> s = ConstrainedSymbol{(:a, :b)}(:c)
ERROR: "Symbol :c is not in the supported set of symbols (:a, :b)."
Stacktrace:
 [1] ConstrainedSymbol{(:a, :b)}(::Symbol) at .\REPL[1]:6
 [2] top-level scope at none:0

julia> s = ConstrainedSymbol(:c)
ConstrainedSymbol{Any}(:c)

julia> Symbol(s)
:c

```

---

<div class="post-metadata">

**Author:** ![rabarar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rabarar/32/6002_2.png) [@rabarar](https://discourse.julialang.org/u/rabarar)\
**Post date:** [December 13, 2018, 9:26pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/8 "2018-12-13T21:26:20Z")

</div>

Doesn’t that look exceeding overly complicated to me and a type with fixed symbols???

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 13, 2018, 9:33pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/9 "2018-12-13T21:33:06Z")

</div>

It seems you have a specific idea in mind. If you can post your attempt, perhaps people can guide you better along your line of thought.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 14, 2018, 12:56am UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/10 "2018-12-14T00:56:08Z")

</div>

It is not currently possible to define a subtype of `Symbol` which only includes some symbols, although the idea has been proposed as a more convenient alternative to the current enum implementation.

---

<div class="post-metadata">

**Author:** ![rabarar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rabarar/32/6002_2.png) [@rabarar](https://discourse.julialang.org/u/rabarar)\
**Post date:** [December 14, 2018, 3:07am UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/11 "2018-12-14T03:07:53Z")

</div>

Thank you. I hope that they do. Without a type-safe symbol subtype the runtime errors are unavoidable.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 14, 2018, 3:23am UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/12 "2018-12-14T03:23:41Z")

</div>

Julia only checks types at runtime…

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [December 14, 2018, 3:12pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/13 "2018-12-14T15:12:07Z")

</div>

As pointed out above, Julia is not type safe in that way anyway. What is your use case? There is probably a better (more Julian) way to do what you’re trying to do. For example, instead of passing around a `mode` argument that is either `:a`, `:b` or `:c` and then have different functionality depending on that mode, it’d probably be cleaner to pass around functions directly, or structs that contain this functionality.

---

<div class="post-metadata">

**Author:** ![rabarar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rabarar/32/6002_2.png) [@rabarar](https://discourse.julialang.org/u/rabarar)\
**Post date:** [December 14, 2018, 3:27pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/14 "2018-12-14T15:27:44Z")

</div>

far enough - For example, assume a function circle(p1, p2, p3, … pn, mode::ModeType) where p1 - pn are parameters and ModeType is ideally just an enumeration of :fill or :outline

the user would call the function circle(p1, p2, …. pn, :fill)

all of the parameters are typed (i.e. p1::P1Type) ensuring that they are checked at runtime. The idea is to limit the mode parameter to the two symbols :fill and :outline - and not for example a misspelled symbol or any other symbol for that matter.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [December 14, 2018, 3:33pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/15 "2018-12-14T15:33:42Z")

</div>

Does this really not work for your application?

```nohighlight
julia> struct Mode{s} end

julia> function circle(args,::Mode{s}) where s
             (args,s ∈ [:fill,:outline] ? s : throw(error("wrong Mode, $s")))
         end
circle (generic function with 1 method)

julia> circle("stuff",Mode{:fill}())
("stuff", :fill)

julia> circle("stuff",Mode{:filler}())
ERROR: wrong Mode, filler
Stacktrace:
 [1] circle(::String, ::Mode{:filler}) at ./REPL[20]:1
 [2] top-level scope at none:0

```

the `circle` method specializes on the `Mode` type parameter, `:fill`. I believe, this should be able to do it.

---

<div class="post-metadata">

**Author:** ![rabarar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rabarar/32/6002_2.png) [@rabarar](https://discourse.julialang.org/u/rabarar)\
**Post date:** [December 14, 2018, 3:43pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/16 "2018-12-14T15:43:13Z")

</div>

that’s the best solution I’ve seen yet. I’d accept that as a readable solution.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [December 14, 2018, 3:54pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/17 "2018-12-14T15:54:35Z")

</div>

Here’s another alternative:

```julia
julia> @enum Mode fill outline

julia> circle(radius::Float64, mode::Mode) = println("$radius - $mode");

julia> circle(3.14, outline)
3.14 - outline

julia> circle(3.14, dashed)
ERROR: UndefVarError: dashed not defined

julia> instances(Mode)
(fill::Mode = 0, outline::Mode = 1)

```

---

<div class="post-metadata">

**Author:** ![rabarar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rabarar/32/6002_2.png) [@rabarar](https://discourse.julialang.org/u/rabarar)\
**Post date:** [December 14, 2018, 4:58pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/18 "2018-12-14T16:58:20Z")

</div>

yes, but the limitation with this alternative is readability - it’s not instantly clear that “outline” is a symbol and not a variable… that’ why I like the explicit :symbol in the construction.

---

<div class="post-metadata">

**Author:** ![f.L0x](https://avatars.discourse-cdn.com/v4/letter/f/bcef8e/32.png) [@f.L0x](https://discourse.julialang.org/u/f.L0x)\
**Post date:** [September 30, 2021, 8:00pm UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/19 "2021-09-30T20:00:45Z")

</div>

Is this idea still active in discusion? I would like it very much

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 1, 2021, 1:22am UTC](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635/20 "2021-10-01T01:22:55Z")

</div>

Hasn’t been implemented, no, nor designed really.

[Next page](https://discourse.julialang.org/t/creating-custom-type-of-enumerations-of-symbols/18635.md?page=2)
