-
Notifications
You must be signed in to change notification settings - Fork 205
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
Support static constraints on type casts and type tests #4074
Comments
What is the advantage of keeping the current behavior? For instance, if for an object If we use What is the advantage of keeping |
One thing is breakage: Type tests and type casts are extremely common, and it is likely to cause massive breakage if the plain If we had no history then perhaps the plain You could also argue that the general kind of type cast (and type test) should use the simplest syntax, and the constrained forms should be requested explicitly by writing an extra character or two. Basically, everybody should be able to use the straightforward syntax; then, as in many other cases, if you want more checks then you'd write more code in order to specify exactly which ones. In any case, So the proposal isn't quite like "keep a lot of expressions around as non-errors at compile time, even though they will throw at run time", it's more like "given that we can't usually know whether a cast is legitimate, let's preserve the existing syntax and semantics, and offer some safer variants using a bit of extra syntax". |
There would be no difference in runtime behavior between Basically, it's a way to express what the author expects of the existing relation between the types, before doing a type check. If that expectation isn't true, they get a compile-time error, so it's an extra validation before doing a type check. The I'm not sure all the variants are really worth it. Especially the distinction between proper and non-proper downcast is going to be very confusing to a lot of people. (And conceptually, going from Another alternative is to have a second, different, syntax and have that only do (not necessarily proper) downcasts, like the often suggested (Which is partly also because I'm not fond of the |
I do not think we have an issue dedicated to this topic, which came up again in dart-lang/sdk#56624. So here we go:
The
as
operator in Dart leaves everything to the developer, as in "I assume you know what you are doing". Similarly, theis
operator will happily test any object against any type.This is fine in the general case because it provides great freedom to inform the type checker about typing properties that are beyond the current static analysis—that is, it's good when we truly know what we are doing.
However, we may well have situations where the intended type casts or type tests are more constrained, e.g., an expression of the form
e as T
may well be written under the assumption thatT
is a proper subtype of the static type ofe
, and it's simply a bug if that relationship doesn't hold. For example, withnum n
in scope, we may at some point evaluate an expression liken as int
, which could be well justified in the given situation, butn as String
is inevitably a bug.Similarly, a type test may well be intended to "be a downcast". For example, with
Object o
in scope, a test likeo is num
may promoteo
tonum
, buto is int?
will not (because promotion will never occur when the target type isn't a subtype of the current type).Consequently, it could be useful to have separate syntactic forms of type queries, adding constraints like "the target type must be a subtype of the static type of the scrutinee", or ".. a proper subtype ..", or ".. a supertype ..", and perhaps others.
Strawman syntax:
as<
respectivelyis<
for "is a proper subtype", and similar operators for other cases. So we'd have this:This kind of syntax could go along with other mechanisms dealing with run-time type queries, e.g., the syntax
as?
which is proposed in #399, where1 as? int
would yield1
and1 as? String
would yield null (rather than throwing). We might then havee as?< T
to make it a proper downcast, and defaulting to null.The text was updated successfully, but these errors were encountered: