-
-
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
RFC: unreachable!
method to express unreachable code explicitly
#4749
Comments
I would prefer to Currently, it is not possible for all the cases to properly use compile time analysis. The first case is a |
@bcardiff I'm not sure what you mean by compile-time unreachable detection, surely if the compiler can prove that it's unreachable then you don't need to tell the compiler that it's unreachable. Unreachable methods like this have a use only because the compiler is not correct in all cases. |
When you have an enum with values If a But if the I think that is safer and doable in the future. Other use cases might involve checking type hierarchy and modules. I agree that a runtime check is useful and make sense. I just don't agree on naming it |
Having to remember to use |
I know the better way is called "exhaustiveness check against Another point, I believe
I think these are fit in Crystal too. |
I'd be for adding |
This is an implementation of crystal-lang#4749.
This is an implementation of crystal-lang#4749.
This is an implementation of crystal-lang#4749.
This is an implementation of crystal-lang#4749.
So should it be |
@straight-shoota Even if we introduce compile-time |
I think the purpose of |
The difference between unreachable code and a compile time assertion in my eyes, is that unreachable code is when checks in the current method make it logically impossible for a branch to be taken. If you rely on other code not to call the method with certain arguments, then that's a runtime assertion and not unreachable code. |
Just letting you guys know that reachability analysis is definitely possible (its done in Scala where the compiler will always warn you if you don't do exhaustiveness checking). It wouldn't be the worst idea to have a look at the compiler source for this detection, because there is an algorithm behind it (the compiler also goes out if its way to optimize case checks with if-else and jump statements) In any case, the One thing to note is that in Scala, there isn't really a specific |
But hold on, doesn't raising an exception already serve as |
Also having |
I would argue for naming the method without the |
It's very similar to |
As @oprypin says: what benefit would I belive we don't need this in Crystal. |
I think that having a unified way to express a common intention is something good. How to assert that that line is unreachable, in order to guide the type system, is something that could be agreed upon project/shard basis. But I don't think it would harm to have a standard way. Regarding the name, I'm ok with using |
You mean like |
Yes @oprypin , I never complain (nor encourage) about an |
All I'm saying is that |
I wont write |
http://smallcultfollowing.com/babysteps/blog/2018/08/13/never-patterns-exhaustive-matching-and-uninhabited-types-oh-my/ goes through how rust seems to be in the process of adding I'd certainly prefer |
Looks like the general consensus is that we want |
As the exhaustive I agree, this is not a new feature, this is just syntactic sugar. But I still think it would be useful, like |
While looking at the Rust macro, I noticed they also have two similar features: In Crystal we have |
Closing per #9988 (comment) |
Sometimes we use
raise "BUG: ..."
to express unreachable code. For example:This
raise "BUG: unreachable"
is important because it is required to inferfoo_bar_flip
's result type asFooBar
correctly, and if not, it is inferred toFooBar?
due to the case of matching nowhen
-clause (it is unreachable case also).There are many unreachable code:
git grep 'raise "BUG: ' | wc -l
in crystal repository shows 81 in fact. So, I think stdlib should provide the method to express such an unreachable code more explicitly thanraise "BUG: "
. This method makes easier to read code. I think such a method is calledunreachable!
.unreachable!
usage:And
unreachable!
spec:unreachable!
raises an error, so this type isNoReturn
.unreachable!
has default message to an error, but we can specify more helpful message.My questions:
I think
unreachable!
is the best name for such a method. However we have some ideas:unreach
(verb?),unreachable
(without bang) and etc... Which do you think the best?Should stdlib have
unreachable!
specific error class? In other words, shouldunreachable!
raiseUnreachableError
-like object? OrException
object simply?What is the default message? I think it is
BUG: unreachable
. Or, shouldunreachable
have the default message?Related issues:
assert_empty_type
and @RX14 suggestedimpossible!
.And I was inspired by Rust's
unreachable!
macro. Thank you.The text was updated successfully, but these errors were encountered: