As your example shows, it seems to not work for unions. That might be problematic when a union contains different enum types with conflicting members, or just plain symbols.
You can use different overloads though. For calling cell this is equivalent to the union type restriction.
enum Auto
Auto
end
def cell(width : Auto = :auto)
pp width.class
end
def cell(width : Int32)
pp width.class
end
cell()
enum WidthOption
Auto
end
alias Width = Int32 | WidthOption
def cell(width : Width)
pp width.class
end
cell(:auto)
It’s just broken for the default argument.
Unfortunately, this is true. Autocasting is a great feature but it only works to some extent. It’s not too bad I think. But eventually, I’d like to remove the symbol datatype so symbol literals can be used exclusively for enum members, which would help improve some use cases for that. But there’s no progress on that and it may actually be harder than it seems.
If I don’t get to fixing it, the workaround is very simple: use Auto::Auto in the default value, but that still allows passing :auto in the method, which I think is the important part you were looking for?
Yes at call site it works. Which is how I got it working in the library so far. But it’s good to know that not working as default value is considered a bug.