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

Add non-null accepting type annotations. #5545

Closed
lrhn opened this issue Sep 28, 2012 · 2 comments
Closed

Add non-null accepting type annotations. #5545

lrhn opened this issue Sep 28, 2012 · 2 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug

Comments

@lrhn
Copy link
Member

lrhn commented Sep 28, 2012

Add a tag to type annotations that make them not accept null.

A traditional syntax would be a suffix '!'. Example:
  int! x = 42;
  x = null;
The latter assignment would fail in checked mode, because while null is assignable to int, it's not allowed by the type assertion "int!".

This is not the same as non-nullable types. A type parameter or type expression is always a type. A type annotation is a type optionally followed by an '!', and it only affects checked mode where the type test
becomes "instance of" instead of "assignable to" for the checked mode type assertions.

It is a common use-case that a method can accept only non-null values. Currently you need to add an extra check for null.

This is ofcourse a special case of more general assertions: http://code.google.com/p/dart/issues/detail?id=3049
but one that is much less intrusive to add.

Issue created because there are wishes for something like this in the discussion of issue #22, which is about more general non-nullable types.

@trinarytree
Copy link

not-nullable maybe be a more reasonable default, with extra annotations reserved for the less common case that you want to permit a null. then instead of this nonsense
int x = null;
x is int; // false, wtf

you could have this
int? x = null; // int? is a union of types int and Null
x is int || x is Null; // true

another option, easy to add right now, would be to at least have an
@nullable annotation in dart:core. yeah, it doesn't enforce anything,
but at least it gives a standard way to communicate programmer intention.

@gbracha
Copy link
Contributor

gbracha commented Aug 25, 2014

Added Duplicate label.
Marked as being merged into #22.

@lrhn lrhn added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report labels Aug 25, 2014
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
This issue was closed.
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). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants