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

Allow Null as the upper bound of a type variable? #33709

Closed
eernstg opened this issue Jun 29, 2018 · 6 comments
Closed

Allow Null as the upper bound of a type variable? #33709

eernstg opened this issue Jun 29, 2018 · 6 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).

Comments

@eernstg
Copy link
Member

eernstg commented Jun 29, 2018

The language specification prevents an upper bound of a formal type parameter from being Null, because Null is a subtype of all other types, and the circularity prevention verbiage (catching cases like <X extends Y, Y extends X>) just says:

It is a static type warning if a type parameter is a supertype of its upper bound.

where static type warning means compile-time error in Dart 2. A similar situation will emerge with a denotable true bottom type Nothing, if such a type is introduced, say, in connection with the introduction of non-null types—the important part is simply that the bound cannot be a type which is a subtype of every other type.

We could consider this to be an accidental special case, in which case we would want to rephrase the specification.

We could also decide that it is useless to specify an upper bound which is the bottom type, because this implies that the bottom type is the only possible actual type argument, i.e., it implies that this type argument will never carry any useful information. Obviously, we'd then have requests for a lint to get rid of it ;-).

This issue is concerned with making a decision on which way to go on that.

@eernstg eernstg added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Jun 29, 2018
@eernstg eernstg changed the title Allow Null as the upper bound of a type variable Allow Null as the upper bound of a type variable? Jun 29, 2018
@eernstg
Copy link
Member Author

eernstg commented Jun 29, 2018

@lrhn, @leafpetersen, do you consider it worthwhile to change the specification in order to allow this?

@leafpetersen
Copy link
Member

My general take is that non-uniformities are bad unless there's a good reason for them. I don't see it as being especially useful, but I see no real reason to disallow it. You can get it indirectly via instantiation, so... I'd say let it fly. If the developer really wants a type variable that can only be Null... who am I to say they're wrong?

@lrhn
Copy link
Member

lrhn commented Jul 2, 2018

Why is <T extends Null>(){}<Null>() different from <T extends int>(){}<int>().
I'd say that Null is no more a supertype of itself than int is, so either we should err in both cases, or in none.

If we want to change the Null is a subtype of every type to ... except itself, then I'm all for it, but I think that's just commentary, and I don't think the implementations have any excuse for disallowing Null even with the current specification. The <: relation is reflexive, Null is not special in that regard. It looks like an implementation bug to me.

@eernstg
Copy link
Member Author

eernstg commented Jul 2, 2018

We don't have a proof of int <: T given that T is declared to have the upper bound int (semantically, we only know that T is some subtype of int, so there's a good reason why we can't prove int <: T), but we do have a proof that Null <: T (until we introduce non-null types, which will make Null a non-bottom type again).

In any case, you both dislike the anomaly, so I'll make an adjustment to the spec.

@eernstg
Copy link
Member Author

eernstg commented Jul 2, 2018

Created this CL to allow Null as a type variable bound.

dart-bot pushed a commit that referenced this issue Jul 3, 2018
We noted in issue #33709 that
the language specification prevents usage of `Null` as the bound
of a type variable, because that makes the bound a subtype of
the type variable itself (which is otherwise a symptom of having
a cyclic declaration like `X extends X`).

This CL adjusts the wording such that it will be possible to use
`Null` (or whatever the denoteable bottom type is called) as a
bound.

Change-Id: I46a5ce6055f3af322a6b93ac38ca2829ce23a26c
Reviewed-on: https://dart-review.googlesource.com/63420
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
@eernstg
Copy link
Member Author

eernstg commented Jul 3, 2018

Closing: As of 4fd0404, the subtype circularity prevention rule in the language specification has be adjusted such that Null is now an acceptable upper bound for a type variable.

@eernstg eernstg closed this as completed Jul 3, 2018
@eernstg eernstg self-assigned this Jul 3, 2018
lrhn pushed a commit to dart-lang/language that referenced this issue Aug 22, 2018
We noted in issue dart-lang/sdk#33709 that
the language specification prevents usage of `Null` as the bound
of a type variable, because that makes the bound a subtype of
the type variable itself (which is otherwise a symptom of having
a cyclic declaration like `X extends X`).

This CL adjusts the wording such that it will be possible to use
`Null` (or whatever the denoteable bottom type is called) as a
bound.

Change-Id: I46a5ce6055f3af322a6b93ac38ca2829ce23a26c
Reviewed-on: https://dart-review.googlesource.com/63420
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).
Projects
None yet
Development

No branches or pull requests

3 participants