Skip to content
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

OptionalExtension does not support null.toOptional #16

Closed
JamesMcIntosh opened this issue May 3, 2021 · 4 comments
Closed

OptionalExtension does not support null.toOptional #16

JamesMcIntosh opened this issue May 3, 2021 · 4 comments

Comments

@JamesMcIntosh
Copy link
Contributor

Hi @tonio-ramirez,

Can you please revert the change made to OptionalExtension as it no longer handles null values with toOptional.

 Optional<T> get toOptional => Optional.of(this);

Should be

 Optional<T> get toOptional => Optional.ofNullable(this);

Missing test case for test/src/extension.dart

    test('null.toOptional == Optional.empty()', () {
      expect(null.toOptional, equals(Optional.empty()));
    });

Many thanks!
James

@tonio-ramirez
Copy link
Owner

null.toOptional is an error under Dart's sound null safety. Support for that construct was removed when support for null safety was introduced.

To be clear, even if the code were reverted to what you suggest, Dart would refuse to run code like null.toOptional, with the following error:

Error: Property 'toOptional' cannot be accessed on 'Null' because it is potentially null.

@JamesMcIntosh
Copy link
Contributor Author

I see what you're saying, this is a more realistic test:

test('nullValue.toOptional == Optional.empty()', () {
  final String? nullValue = null;
  expect(nullValue.toOptional, equals(Optional.empty()));
});

@tonio-ramirez
Copy link
Owner

The problem isn't the test. It's that the construct isn't valid. You can't invoke methods/properties of a reference to a nullable type. The test you suggest would also fail to run, with the same error.

@JamesMcIntosh
Copy link
Contributor Author

So sad! This makes the extension literally half as useful.
I had a good read of the GitHub discussion around this (dart-lang/language#392 & dart-lang/language#393) and can see why they chose it in regards to shorting and call ambiguity but this is the perfect case where it is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants