Skip to content

Commit

Permalink
Revert Optional<T extends Object> to Optional<T> (#667)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cbracken authored Nov 5, 2020
1 parent 4bc5aa3 commit ddf6491
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/src/core/optional.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import 'dart:collection';
/// Use Optional as an alternative to allowing fields, parameters or return
/// values to be null. It signals that a value is not required and provides
/// convenience methods for dealing with the absent case.
class Optional<T extends Object> extends IterableBase<T> {
//
// TODO(cbracken): Consider making this Optional<T extends Object>.
// See: https://github.com/google/quiver-dart/issues/666
class Optional<T> extends IterableBase<T> {
/// Constructs an empty Optional.
const Optional.absent() : _value = null;

Expand Down

0 comments on commit ddf6491

Please sign in to comment.