-
-
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
Compile error when summing empty Array of Enum that doesn't start at 1 #12820
Comments
This doesn't really work.
That's totally possible. It just won't lead very far for the reason mentioned above. But you can implement
Again, for the reason mentioned above, this won't happen. |
Gotcha, @straight-shoota. I thought that because my All the ideas proposed sound plausible to me. :D Definitely not a bug then. Closing this, sorry for the noise and thanks a lot for the suggestions. |
@straight-shoota if Enum does not implement generic arithmetic, then I am surprised that sum is even a method on Enum at all. I would have expected a compiler error that sum is not defined. Instead you get a confusing error that zero is not defined. |
I think the catch is that there isn't, as it's called on the |
Oh my apologies. For some reason I misread and I was thinking sum was defined on the Enum, but this is on Array. Even so, the error message is not clear to me, and I think there might be a nicer way to present it. |
And the addition operation itself. Yes, these kind of compile time errors are difficult to understand, because the actual error happens somewhere down the stack and it doesn't immediately point out the problem. It's hard to improve that though. |
I think this brings us back to Interestingly, I was looking for the old PR where asterite was working on it and he gave this exact example: #11756 (comment). I still contend that while we couldn't come to full agreement there, we still need to do something about |
I was about to comment exactly this but I'm getting tired of sounding like a broken machine. I think we should do this. The simplest thing we can do is to remove the flag and always show the full error trace. Unfortunately I might be the only core team member who thinks that way. |
Mind that I am new in the Crystal world, but why can't the compiler tell me Indeed, as @asterite wrote on his original issue, this error is so cryptic to a beginner like me. The compiler should report an error on my code, not on |
There are a couple of issues with that:
For point 2, we want something like: module Enumerable(T)
def sum where T < Additive
end
end that is, putting a restriction on the And even after making it match an In Crystal you get an error similar to what you would get in Ruby: "Hey, you need to implement a Once you do that I think the compiler will tell you to implement That said, this can definitely be improved, it's just that it's not clear how. |
That is #3298. My preferred syntax would be something like: # no bounds = unconstrained generic
module Enumerable(T) forall T; end
# partial specialization
module Enumerable(T) forall T <= Additive
def sum; end
end But I don't like the idea that types must explicitly |
Bug Report
Playing around with the language on Advent of Code, I noticed that when you call
Enumerable#sum
on anArray
of anEnum
that starts at 1 the compiler errors out because it cannot figure out the zero-value of theEnum
.Example:
Running this will trigger:
I think the error makes sense. I would love Crystal to either let me tell it what's the zero-value of my Enum. Or it could assume it's the first.
The text was updated successfully, but these errors were encountered: