-
Notifications
You must be signed in to change notification settings - Fork 12.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
Contextual type assertion operator #2876
Comments
<?>
So in contrast to
|
Correct |
Instead of
I would be really interested in using your
|
Function arguments are contextually typed by the corresponding parameter types, so |
👍 |
Indeed, but my case is different. I would like to be able to perform the same, but without mentioning the corresponding parameter type.
|
👍 for <?> |
General take on this was that users are unlikely to be able to correctly reason about what this operator is doing (given that most people don't work on compilers all day like I do 😃 ). Some better solution for an "automatic downcast" would be neat, but it's hard to imagine what that would be. |
A contextual type assertion expression,
<?>
, is like a regular type assertion (e.g.<string>
), but asserts to the contextual type of the expression.About half of all type assertions can be rewritten this way with no loss of type safety.
Example:
It is an error to use a contextual assertion when no contextual type exists:
This should nearly eliminate assertions to
<any>
due to people not wanting to type out large type literals.The text was updated successfully, but these errors were encountered: