On Wed, Apr 26, 2017 at 7:50 PM, Luis Lavena <
luisl...@gmail.com> wrote:
> Hello,
>
> On Wednesday, April 26, 2017 at 2:00:29 PM UTC-3, Roger Pack wrote:
>>
>> Question on enums, in this example in the docs, it seems to imply that a
>> magic "unknown color" could occur, even if all the colors are in the Case
>> statement, is that possible?
>
>
> An unknown Color can occur if Color.new is used with an integer value, as
> described in the documentation:
>
>
https://crystal-lang.org/api/0.22.0/Enum.html
OK thank you.
"Values that don't correspond to an enum's constants are allowed:"
Hmm just debating this...in such a strongly typed language as Crystal
I might suggest this behavior (unknown enums) be made
optional...typically, users of an Enum for parameter passing don't
even know/want to to know what the integer values backing each
constant are. They just want to use the constant. And if only values
that correspond to constants are allowed, then case statements for
Enum's wouldn't "practically all" have to have that ending else block
(plus then the compiler could warn more easily "you missed one in that
case statement" or what not).
Anyway just my $0.02 :)
Next question: Is there a way in Crystal to import constants?
My use case is that I want to create a parameter for a method of a
class, but specifying and using an Enum seems to require multiple
constant derefs, here:
class Y
enum MethodOption
Red
end
def method(c : MethodOption)
puts "got #{c}"
end
end
a = Y.new
a.method(Y:: MethodOption::Red) # is there any way to say just "Red"
here, and it work?
Thanks all. :)
-roger-
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Crystal" group.
> To unsubscribe from this topic, visit
>
https://groups.google.com/d/topic/crystal-lang/YJbUG1lQP3Q/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
>
crystal-lang...@googlegroups.com.
> To post to this group, send email to
crysta...@googlegroups.com.
> Visit this group at
https://groups.google.com/group/crystal-lang.
> To view this discussion on the web visit
>
https://groups.google.com/d/msgid/crystal-lang/7162a9fe-0aa0-43c4-97e5-02296df95a21%40googlegroups.com.
>
> For more options, visit
https://groups.google.com/d/optout.