-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: spec: add nothing
type for functions that don't return
#69591
Comments
Every language change is a cost/benefit decision. This proposal has a cost of adding a new keyword and making the type system more complicated. The benefit is that certain kinds of code can skip writing some return statements. Personally I don't think the cost is worth the benefit. |
Typically, the benefit of an uninhabited type is that the type checker verifies that a function doesn't return, rather than skipping returns. It's particularly helpful with functions involving non-trivial control flow that need to guarantee there is no path to the end of the function, due to something like a missing However, an uninhabited type in Go would have a great many problems. It would not be sufficient to treat This becomes especially complicated when type parameters are involved. Can |
There's an alternative that might avoid a bunch of those problems: Don't use a real type. Instead, use a special syntax that indicates that the function never returns. This is what Rust does, i.e. In terms of |
to me this looks like #30582 but awkwardly in the type system rather than as a compiler hint. awkwardly because it relies on possible code paths rather than just types. |
Based on the discussion above, and the emoji voting on #69591 (comment), the benefit of this change does not seem to be worth the cost. Therefore, this is a likely decline. Leaving open for four weeks for final comments. |
You can also write |
No change in consensus. |
Go Programming Experience
Experienced
Other Languages Experience
Go, Ruby, JavaScript, C, Python, Elixir, Kotlin, Dart
Related Idea
Has this idea, or one like it, been proposed before?
As far as I know, no.
Does this affect error handling?
Not directly, but it can help with certain existing error handling patterns.
Is this about generics?
No.
Proposal
I propose adding a new builtin
nothing
type,nothing
would only be valid to use as a return type of a function and would indicate that the function never returns because every code path in the function either stays in an infinite loop forever, panics, or calls a function that returnsnothing
.This would primarily be helpful for writing helper functions for code that passes data around via panic and recover. For example, in a recent project of mine I used this mechanism in a recursive descent parser to get unrecoverable errors back up to the top of the recursion without needing to return and check them at every step of the recursion. To help with this, I wrote a number of methods along the lines of
func (p *parser) raiseUnexpectedToken(tok scanner.Token)
that simply construct the correct error type and then panic it, and that panic is then recovered at the top of the parser's recursion. However when I use those methods instead of panicking directly, which is more error prone as passing the wrong thing will prevent the recover from catching It, Go can't tell that the code won't continue after the call site. This leads to me having to put dummyreturn
statements after every call to any of those helper functions. Not a huge deal, but certainly an annoyance, and it makes reading the code more confusing as it looks like the code returns there.A
nothing
builtin would also have the benefit of being able to enforce that the function actually doesn't return, thus making the implementation of functions for that purpose safer.Example
Reflect
reflect
is the biggest complication that I can think of with this. For example, code likewould have to do something, despite it not being legal to do
new(nothing)
in regular Go code. Simply panicking is probably the simplest approach.nothing
might also need its ownreflect.Kind
.Bikeshedding
I'm very much not stuck on
nothing
as the name. Some alternatives that I also kind of like arenoreturn
andpanics
.Language Spec Changes
No response
Informal Change
The
nothing
predeclared identifier can be used as the sole return type of a function to indicate that it never returns. If a function is marked with this, every code path in the function must either loop forever, panic, or call a function that also returnsnothing
.Is this change backward compatible?
Yes.
Orthogonality: How does this change interact or overlap with existing features?
It fits well with
panic()
.Would this change make Go easier or harder to learn, and why?
Mildly harder, perhaps. It's not a very complicated feature.
Cost Description
reflect.Kind
, potentially.nothing
is for.Changes to Go ToolChain
Anything that parsed Go code would be affected.
Performance Costs
Very minor increase to compile-time cost. No increase at all to runtime.
Prototype
No response
The text was updated successfully, but these errors were encountered: