-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: Go 2: merge Type Assertion notation x.(T) into Explicit Conversion notation T(x) #33480
Comments
The biggest advantage of the current notation: type conversions never fail, type assertions do. If I understand this proposal correctly, you are shifting the notation so that the ", ok" form is required for type assertions. If we make that required, then we no longer need to distinguish the cases with a different syntax. But it would be rather annoying to have to rewrite all existing simple type assertions to use the ", ok" form. |
Thanks for reading this proposal.
With the current notation, we have two way to know type assertion has not succeeded : with the boolean return parameter and by run-time panic. This proposal just removes the run-time panic but keeps a distinction between type conversions which always succeed and type assertions which don't.
Since we keep the current syntax for backward compatibility, it may be done in a smooth transition, and give an opportunity for developers to check there's no unsafe type assertions in their code. To be honest, programming languages I know have an unsafe cast operator which generates run-time error, even if their documentation warns against them : Java typecast generate ClassCastException, kotlin has unsafe "as" and safe "as?", swift with the conditional "as?" and forced "as!" |
The current way that Go does it works very well I think, and I think they both translate to natural language well.
|
If we are going to keep the current syntax, which I agree makes sense, then I don't see the benefit of this change. We don't need two different ways to do the same thing. Your argument is, I think, that conversions and type assertions are similar, and should be expressed similarly. But they aren't all that similar, and even in this proposal they are expressed somewhat differently, albeit less differently than they are expressed now. I don't really see what we gain by making this change. |
For the reasons given in the last comment, this is a likely decline. Leaving open for a month for final comments. |
There were no further comments. |
Go defines two different notations for similar concerns : type checking and changing
v = T(x)
for static types (at compile-time)v, ok = x.(T)
and Type switchesx.(type)
for dynamic types (at run-time)Motivation
I think type assertions and type switches notations are surprising, especially for Go newcomers, not standard, and almost redundant with Conversions notation :
x.(T)
notation isn't common in popular programming languages, evokes methods with return parameters or selector notation for interface and struct embedding. Citing Ian Lance Taylor : The "." operator is heavily overloaded in Go. […] There is no special reason for this [use in type assertion] except that it is unambiguous and looks good on the page.v, ok = x.(T)
andv = x.(T)
) is said to be similar to maps but is actually different : when retrieving an element from a map, both notationselem, ok = m[key]
andelem = m[key]
return zero value if key isn't found, while for type assertion,v = x.(T)
triggers a run-time panic if the assertion is false.Proposal
Merge these notations into one unique notation
T(x)
modeled on Conversion notation :v = T(x)
and is used when dynamic type assertion isn't needed : x isn't of interface type, or if type of x and T are both interfaces and T is a subset of x's interface type (to satisfy Assignability rule in conversions)v, ok = x.(T)
becomesv, ok = T(x)
and is used otherwise. Boolean return parameter is not optional anymore.switch x.(type) {}
becomes :switch type(x) {}
Benefits
Drawbacks
v = x.(T)
won't be possible anymore. Reasons :Exemple
The text was updated successfully, but these errors were encountered: