Null-Aware Infix Operators in Dart - likely unneeded code on line "return shadow + (other ?? 0) as T"
gaddlord opened this issue · comments
Having:
T? operator +(final T? other) {
final shadow = this;
if (shadow != null) {
return shadow + (other ?? 0) as T;
} else {
return null;
}
}
and the test:
test('Test null + operator', () {
int? var1;
var1++;
var1 += 1;
var1 = var1 + 1;
var1 = 2;
var1++;
var1 += 1;
var1 = var1 + 1;
expect(var1, 5);
}
The test runs successfully but with code coverage we can see that the line
return shadow + (other ?? 0) as T;
never gets called which leads me to the speculation that shadow always equals to NULL when the extension is called and the extension is not "attached" if T is not null.
@gaddlord as far as i understand, in your example precedence gets default int + operator.
int and double are not strictly classes, because they are implemented on the platform-specific basis, thus it can be considered a corner case.
well, you still can get expected behavior with explicit spec, like on screenshot