Hacker News new | ask | show | jobs
by mraleph 2487 days ago
It's a non-null assertion operator which would throw an error if `e1 == null`.
1 comments

Would the operator itself throw the error, or trying to access `[e2]` on null?

I never used Dart, I would assume it's basically a cast from `Optional<SomeType>` to `SomeType`, so wouldn't throw an exception itself

The operator itself would throw an error.

Dart is planning to have a sound non-nullability which means that if something has static type `SomeType` then it is guaranteed to be a value that is actually `SomeType` and not something else.

Thus you can't take `SomeType?` and just cast it to `SomeType` because `SomeType?` can be null and `SomeType` can't be null.

This means this cast has to perform a check and throw if your value is null.

This also means that if you have `e1` and `e1` has non-nullable static type then `e1` would never be null in the NNBD world - which is a very good property (e.g. for optimizations).

Interesting! So you were right to call it an assertion rather than a cast