casting a null value throws a NullPointerException #3455
Labels
area-language
Dart language related items (some items might be better tracked at github.com/dart-lang/language).
Milestone
This issue was originally filed by [email protected]
Per the spec on casts[1], the following code throws NullPointerException:
var x = null;
String s = x as String;
as does this following (type-correct) code:
String x = null;
String s = x as String;
This is surprising to me - null is a legal value for the String type.
It's also different to how e.g. Java and C# behave.
I can see use cases for asserting non-nullity, but this is orthogonal to casting (unless non-nullable types are added, see issue #22).
One counterargument is that the specified behaviour is consistent with 'is'.
I'm used to that inconsistency from Java (spelling it instanceof reduces the confusion there) so I'm not sure how confusing it is.
[1] http://www.dartlang.org/docs/spec/latest/dart-language-specification.html#h.4pppxtxtu3v2
The text was updated successfully, but these errors were encountered: