Sdk: Problem with null safety and extensions

Created on 18 Dec 2019  路  3Comments  路  Source: dart-lang/sdk

Hey guys!
I am playing with nullsafety.dartpad.dev and found something that is really annoying. I know the feature for NNBD isn't ready yet, but this is really intended?

void main() {
  int? value = 0;   
  print(value?.doubleAsString() ?? "empty");  
}

extension on int {  
  String doubleAsString() => "${this + this}";  
}

Gives error:
image

Is this really intended? Will this not break all the extensions feature?
Thinking on a real world development this is definetly a problem, since we having a lot of libraries out there based on extensions already (like https://github.com/leisim/dartx) that will stop serving its purpose once we have the NNBD feature released and breaking it...

NNBD area-analyzer type-bug

All 3 comments

This should work.

The e?.method() should work the same as (tmp = e) == null ? null : tmp.method() where tmp has the same type as the static type of e, and that type should get promoted so that tmp.method is called on the promoted type.

Moving this to the SDK repository as a bug against the analyzer.

cc @scheglov @stereotype441 @johnniwinther

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DartBot picture DartBot  路  3Comments

jmesserly picture jmesserly  路  3Comments

ranquild picture ranquild  路  3Comments

DartBot picture DartBot  路  3Comments

bergwerf picture bergwerf  路  3Comments