-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[Breaking Change Request] Deprecate CastError, make everything a TypeError. #40763
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
Comments
cc @Hixie @matanlurey @dgrove @vsmenon for review and approval. |
lgtm for dart |
LGTM. Can we get rid of |
Now, |
Ping @Hixie |
LGTM if this is to enable a Web optimization. If it wasn't for the optimization, I'd probably suggest keeping TypeError for implicit cast errors and CastError for explicit cast errors, because to the developer they are still distinct, even if the compiler/language doesn't really care about the distinction any more. That said, this seems very minor and I don't feel strongly at all. |
Approved |
Landed. Close as discussed in SCRUM |
Boolean conversion now throws a TypeError, not CastError. See: * #40317 * #40763 Presubmit: https://critique.corp.google.com/#review/301195917 Change-Id: I76e8aa4a849eb519e47c80f1b4873032a05ad636 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/139402 Reviewed-by: Nicholas Shahan <[email protected]> Commit-Queue: Mark Zhou <[email protected]>
CastError is deprecated. See #40763 for details. Change-Id: If00963e68987a259396c4b5a0cd6d703bc7ac76c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/140885 Reviewed-by: Lasse R.H. Nielsen <[email protected]>
Intended Change
Make every platform operation currently throwing a
CastError
instead throw an object implementing bothTypeError
andCastError
. (Effectively make all implementations ofCastError
also implementTypeError
).Deprecate
CastError
and recommend that everybody usesTypeError
instead.Implementations are allowed to use a class implementing
CastError
when they throw aTypeError
, so they can decide to only have one class.Eventually remove
CastError
from the platform libraries (possibly along with other errors classes that can no longer happen, primarilyAbstractClassInstantiationError
andFallthroughError
).Rationale
Currently the Dart runtimes throw
TypeError
in some situations andCastError
in other situations.In Dart 1, the distinction was clear:
TypeError
was anAssertionError
and was only thrown in checked mode when a type assertion failed, andCastError
was thrown when anas
operation failed.In Dart 2. that distinction was no longer meaningful. Because of sound typing, a type assertion can no longer fail. If there is a risk that it can fail at run-time, the compiler inserts an implicit cast. It is as if your
int x = dynamicValue;
was converted toint x = dynamicValue as int;
.Except that the explicit cast throws
CastError
and the implicit cast throwsTypeError
. Maybe.The language specification no longer mentions
CastError
, simply stating that a failedas
cast is a dynamic type error, which means that it should be throwing aTypeError
.When compiling to JavaScript, it's often necessary to convert native exceptions to Dart errors, and having to figure out whether to use a
TypeError
or aCastError
is an extra unnecessary overhead.All in all, the
CastError
does not carry its own weight.See also dart-lang/language#787.
Expected impact
No initial impact. Requires some later migration from catching
CastError
to catchingTypeError
before theCastError
can be removed.Catching errors:
By making all errors that are actually thrown implement both
CastError
andTypeError
, any code which currently catchesCastError
orTypeError
will still catch the same errors. The only difference would be in a situation where atry
/catch
statement attempts to distinguish between the two error types by catching both, withTypeError
first, and it will now trigger the former catch clause instead of the latter.Throwing errors:
No known code outside of the Dart SDK platform libraries are implementing or extending
CastError
(orTypeError
). No known code outside of the Dart SDK throws a newCastError
. (There is code throwing aTypeError
).Migration:
Code currently catching
CastError
should instead catchTypeError
.This is probably primarily testing doing things like
expect(..., throwsA(isCastError))
.The
CastError
is marked as deprecated, so some clients might want to migrate quickly to avoid warnings. Deprecation is not a breaking change.The text was updated successfully, but these errors were encountered: