So I’ve made some updates to SumTypes.jl which I believe solves your problems posted here.
I can’t use the names I want to use because it puts the names directly in the namespace. I need to go back and forth between strings and enums easily.
SumTypes.jl now lets you avoid polluting the name space:
julia> using SumTypes
julia> @sum_type Fruit begin
apple
banana
orange
end hide_variants=true
Fruit
julia> apple
ERROR: UndefVarError: apple not defined
julia> Fruit'.apple # note that I used Fruit' not Fruit
apple::Fruit
julia> let (;orange, banana) = Fruit'
orange
end
orange::Fruit
Override Base.show for enums doesn’t appear to work.
Should work fine with SumTypes:
julia> Base.show(io::IO, f::Fruit) = @cases f begin
apple => print(io, "apple")
banana => print(io, "banana")
orange => print(io, "orange")
end
julia> Fruit'.apple
apple
I don’t want to put it in a little module by itself to namespace it because I can’t use that same name for both the module and the enum which causes confusion that I have to use X for type but Y.z for values.
I agree, so I didn’t do that.
julia> typeof(Fruit'.apple) === Fruit
true
I found this online: Encapsulating enum access via dot syntax But that results in allocations just to reference MyEnum.value
Don’t have that problem here:
julia> @btime Fruit'.apple
1.258 ns (0 allocations: 0 bytes)
apple
julia> @code_typed Fruit'.apple
CodeInfo(
1 ─ %1 = Base.getfield(x, f)::Fruit
└── return %1
) => Fruit
I don’t need nor want the values in the type system, so that’s not an option.
I use values, not types:
julia> Fruit'.apple isa Type
false