-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
case enum #285
Comments
What about: case color : Color
when Red
...
when Green
...
when Blue
...
end |
@mverzilli I like that syntax. In fact, we can use it beyond enums to avoid repeating the namespace. So we could do this: class Foo
A = 1
B = 2
C = 3
end
case foo : Foo
when A
...
when B
...
when C
...
end To avoid having to write I'm not totally convinced about it, though, because the colon is starting to have many meanings, but it looks short and readable. |
Uhhh.. Having a "use namespace" as postfix-colon-in-case seems weird to me :) |
Actually, I thought of Then for enums one can assume when clauses refer to Foo's possible values. I agree that the extrapolation of this idea to classes may feel weird. I kind of borrowed the idea from Scala and its case classes, but of course that's far from being a simple switch over enum values :). |
I don't know the semantics of Crystal so deeply yet to know if this can be implemented, yet I foresee at a high level a rising pattern of implicit type annotations, see also the relevant issue #284, where properties can be "type-annotated" via a single colon too (not sure if they can actually, but it would be amazing to bring all these issues together under a common type annotation single colon) :) |
? |
This conflicts with implicit calls, doesn't it? It's currently |
@vendethiel unless every is a keyword :-P. |
enum case. We can't do subtype checking. Would love to see enums being used as ADTs :) |
What @bcardiff says is maybe the best thing, because the compiler tries to be as smart as possible based on the type of the variable. It's the hardest thing to do in the compiler, but programmers will be happier :-) |
What about adding other control sequence: |
Now that we added enum query methods, it can be done like this: color = Color::Red
a = case color
when .red?
1
when .green?
2
when .blue?
3
end |
Could this be documented? The book shows an example like this, but there isn't a clue that this is automatically derived. Also, after this discussion I'd expect an compile error, but running this code compiles fine and prints enum Color
Red
Green
Blue
end
color = Color::Red
x = case color
when .red?
1
when .green?
2
end
printf("%s : %s\n", x, typeof(x)) (Related: #1846) |
We have enums and you can use
case
with them:Enums are great because, unlike symbols, they are type-safe. They are like a group of limited and documented symbols. You can't accidentally misspell them. And you can use them as restrictions.
However, the above
case
has a few issues:a
's type is nilable, because there's an implicitelse
part withnil
. Ideally the compiler should be smart and know there are no other possible values. This is partly because the compiler blindly transforms thecase
into a series ofif-elseif
, but also, because of how our type inference works,color
might eventually get another type.I'm thinking about having something like this:
Possibly with a modifier to make it
final
.The text was updated successfully, but these errors were encountered: