I’m trying to redefine Enum.parse?
but re-use the previous parsing in order to add more possible parsable values. The issue is that neither previous_def
nor super
works, for reasons that make sense. previous_def
doesn’t work because it’s not really meant for this situation (rather, it’s used when you’ve already defined the method for the particular type), and super
doesn’t work because of how Enum works (see this issue). However, that leaves me with no choice but to reimplement the supertype method, which isn’t ideal. Here’s an example with super
:
enum Something
Alphabet
Numeric
def self.parse?(string : String) : self?
result = super string # => Error: can't use Enum in unions yet, use a more specific type
if result.nil?
case string
when "abc"
result = Alphabet
when "123"
result = Numeric
end
end
result
end
end
# I want these both to work
s = Something.parse? "abc"
t = Something.parse? "numeric"
p! s
I think the compile-time error message is indicative of the kind of problem it is, but not the underlying issue (which is that super
doesn’t work on Enum
because of how Enum
works).
Really, the reason I’m explaining all this is that I hope I’m missing something. Is there a solution I haven’t considered?