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

Revert Optional<T extends Object> to Optional<T> #667

Merged
merged 1 commit into from
Nov 5, 2020

Conversation

cbracken
Copy link
Member

@cbracken cbracken commented Nov 5, 2020

Optional is intended as a substitute for non-nullable values. Ideally,
it should have a generic type of T extends Object in order to prevent
non-sensical declarations like Optional<Foo?>. Such declarations are
unlikely to happen intentionally, but one could imagine the case of an
Optional used in another generic class, where T is a nullable type.

In #652, we prevented such uses by declaring Optional as
Optional.

This turns out to be a significant breaking change, since previously, T
implicitly extended dynamic and therefore checks like the following
didn't produce any analysis errors:

if (obj is Optional) {
  return obj.value.someMethod();
}

where someMethod is a method defined on T.

A survey of the Google codebase suggests this would be a fairly
extensive breaking change. Given that Optional makes little sense once
null-safety is enabled in the language by default, the simpler change
may simply be to leave as-is and deprecate the class once null-safety
lands in the language.

It appears that there are no checks on the return type of transform,
and this seems significantly unlikely, therefore we leave that change in
place.

This partially reverts 28301f9.

Related: #666
Related: #606

Optional is intended as a substitute for non-nullable values. Ideally,
it should have a generic type of `T extends Object` in order to prevent
non-sensical declarations like `Optional<Foo?>`. Such declarations are
unlikely to happen intentionally, but one could imagine the case of an
Optional<T> used in another generic class, where T is a nullable type.

In google#652, we prevented such uses by declaring Optional as
Optional<T extends Object>.

This turns out to be a significant breaking change, since previously, T
implicitly extended `dynamic` and therefore checks like the following
didn't produce any analysis errors:

    if (obj is Optional) {
      return obj.value.someMethod();
    }

where `someMethod` is a method defined on `T`.

A survey of the Google codebase suggests this would be a fairly
extensive breaking change. Given that Optional makes little sense once
null-safety is enabled in the language by default, the simpler change
may simply be to leave as-is and deprecate the class once null-safety
lands in the language.

It appears that there are no checks on the return type of `transform`,
and this seems significantly unlikely, therefore we leave that change in
place.

This partially reverts 28301f9.

Related: google#666
@cbracken cbracken added the nnbd label Nov 5, 2020
@cbracken cbracken requested a review from yjbanov November 5, 2020 18:59
@google-cla google-cla bot added the cla: yes @googlebot is happy with the CLA status of this PR label Nov 5, 2020
Copy link
Member

@yjbanov yjbanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@cbracken cbracken merged commit ddf6491 into google:null_safety Nov 5, 2020
@cbracken cbracken deleted the tweak-optional branch November 5, 2020 19:20
cbracken added a commit that referenced this pull request Nov 6, 2020
Optional is intended as a substitute for non-nullable values. Ideally,
it should have a generic type of `T extends Object` in order to prevent
non-sensical declarations like `Optional<Foo?>`. Such declarations are
unlikely to happen intentionally, but one could imagine the case of an
Optional<T> used in another generic class, where T is a nullable type.

In #652, we prevented such uses by declaring Optional as
Optional<T extends Object>.

This turns out to be a significant breaking change, since previously, T
implicitly extended `dynamic` and therefore checks like the following
didn't produce any analysis errors:

    if (obj is Optional) {
      return obj.value.someMethod();
    }

where `someMethod` is a method defined on `T`.

A survey of the Google codebase suggests this would be a fairly
extensive breaking change. Given that Optional makes little sense once
null-safety is enabled in the language by default, the simpler change
may simply be to leave as-is and deprecate the class once null-safety
lands in the language.

It appears that there are no checks on the return type of `transform`,
and this seems significantly unlikely, therefore we leave that change in
place.

This partially reverts 28301f9.

Related: #666
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes @googlebot is happy with the CLA status of this PR nnbd
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants