This question has some lengthy background where I explain my understanding of the problem. If you already know how the rules Julia uses for using values inside type parameters you can skip to the end:
My understanding is that currently in Julia, if you want to use a value in a type parameter, that value must be a bit value satisfying isbits(v) == true
. If that works, then for instance you can do
julia> isbits(5)
true
julia> Val(5)
Val{5}()
julia> typeof(Val(5)) == Val{5}
true
This also works for types which wrap isbits
types
julia> struct MyInt a::Int end
julia> isbits(MyInt(5))
true
julia> Val(MyInt(5))
Val{MyInt(5)}()
On the other hand, non bit types can’t be used in type prameters
julia> isbits("hi")
false
julia> Val("hi")
ERROR: TypeError: in Type, in parameter, expected Type, got String
Stacktrace:
[1] Val(::String) at ./essentials.jl:728
[2] top-level scope at none:0
an exception to this rule is Symbol
s which as far as I understand have been special-cased such that they are allowed in type parameters
julia> isbits(:hi)
false
julia> Val(:hi)
Val{:hi}()
However, it seems unlike isbits
types, I can’t wrap a Symbol
in another type and have that work in a type parameter:
julia> struct MySymbol s::Symbol end
julia> Val(MySymbol(:hi))
ERROR: TypeError: in Type, in parameter, expected Type, got MySymbol
Stacktrace:
[1] Val(::MySymbol) at ./essentials.jl:728
[2] top-level scope at none:0
The Actual Question
Is there any way for me to tell the compiler that a value of type
struct MySymbol s::Symbol end
is safe to put in a type parameter? Or is the special casing for Symbol
not replicable at the user / package level?