Skip to content

Non-nullable type annotation syntax #27231

Closed

Description

@munificent

This is the tracking bug for the real implementation of the postfix ? syntax to mark a type annotation as nullable as part of supporting non-nullable types (#28619). See the "Syntax" section of the proposal for details.

Since we're still only in the prototype phase, consider this bug to be on hold. If we decide to go ahead and ship a real implementation, I'll update this.

Flag

While this is being implemented, to ensure we don't expose users to inconsistency in our tools, it should be put behind a flag named nnbd.

Tracking issues

Activity

  1. added
    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.
    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).
    area-metaCross-cutting, high-level issues (for tracking many other implementation issues, ...).
    type-enhancementA request for a change that isn't a bug
    on Sep 2, 2016
  2. crelier commented on Sep 2, 2016

    @crelier
    Contributor

    Just curious, why was '?' chosen? It seems very counter-intuitive to me, since a question mark implies some kind of optionality. In this case, we rather want to express a restriction. A more assertive exclamation mark would be more appropriate, in my opinion, but I did not study possible grammar conflicts, if that is the reason of this choice.

  3. lrhn commented on Sep 3, 2016

    @lrhn
    Member

    The question mark means that the type is nullable/optionally null/union of type and Null, and its absence means that the type is just itself.

  4. donny-dont commented on Sep 3, 2016

    @donny-dont

    Any chance union types will come along with this? They seem to be natural fits.

  5. dynaxis commented on Sep 4, 2016

    @dynaxis

    This seems just an experiment for now. But I'd like to note that there might be needs for addition of APIs to the standard library. For instance, Kotlin has filterNotNull and mapNotNull on nullable sequence or collection, which return one with non-nullable element type. Dart's Stream and collections would be better to have such new APIs. They can definitely be implemented in user codes, but are really clumsy to do so.

  6. crelier commented on Sep 4, 2016

    @crelier
    Contributor

    @lrhn Oh, I see. The title is misleading and the text does not clarify. It should actually be "Nullable type annotation syntax" instead of "Non-nullable type annotation syntax".

  7. zoechi commented on Sep 5, 2016

    @zoechi
    Contributor

    @tantumizer

    "is-not-null" assertion operator.

    This is shorter

    return x ?? (throw new NullPointerException);

    See also #24892

    I find x!! a bit cryptic.
    If the return type for the function that contains this code is a non-nullable type it should throw in checked mode anyway and make the check and throw redundant.

  8. lrhn commented on Sep 5, 2016

    @lrhn
    Member

    @tatumizer

    Good questions, some not decided yet.

    Optional parameters: If nothing else changes, you'll have to make them nullable by adding a ? to the type. After all, that is their type. We may consider allowing you to make them non-nullable if they have a non-null default value. If we do that, we should probably also change how default parameters work by making an explicit null argument be equivalent to not having the argument at all. There is some design-space here to explore.

    For a nullable var, it's not really a problem as we are moving towards Strong Mode where var means to infer the type. You can write dynamic? but since null is already assignable to dynamic, I think dynamic? will just mean the same as dynamic (so we may disallow it entirely, there is no reason to have two ways to write the same thing). Same for Object and Null.

    Too many question-marks. Likely true, but they are consistently about null (except for the original ?: conditional operator).

    A not null assertion (that is: take nullable value, throw if it's null, otherwise we know the type).
    That could just be x as Foo if the type of x is Foo?. We will have to change as to not allow null for non-nullable types, you would then write x as Foo? to get the current behavior.
    We are also considering shorthands to quickly coerce a value to an expected type. Maybe x! will check for null if x has type Foo? and it's used in a position where a (non-nullable) Foo is needed. Maybe that's just being too clever by half.

  9. zoechi commented on Sep 5, 2016

    @zoechi
    Contributor

    If you have

    String foo = map["foo"];
    

    then ?? throw new NullPointerException(); is redundant because String already is non-nullable and checked mode should throw.

  10. 53 remaining items

  11. donny-dont commented on Jan 14, 2017

    @donny-dont

    @munificent the link to the dart2js bug is wrong up top. Off by one error 😉

  12. added
    closed-obsoleteClosed as the reported issue is no longer relevant
    on Jun 25, 2018
  13. osdiab commented on Jun 27, 2018

    @osdiab

    Is this not happening? :/ I find this to be a really useful concept for ensuring type safety of my code, and along with the lack of union types is making me wary of switching from TypeScript/React Native to Dart/Flutter in future projects.

  14. munificent commented on Jun 27, 2018

    @munificent
    MemberAuthor

    It's not happening yet. Moving Dart to strong mode is a necessary pre-condition for non-nullable types. We are doing that in Dart 2. We hoped to get non-nullable types in at the same time, but it proved to be too big of a change to fit into 2.0, so it's going to have to wait until a later version.

  15. added
    status-blockedBlocked from making progress by another (referenced) issue
    and removed
    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).
    closed-obsoleteClosed as the reported issue is no longer relevant
    on Jul 11, 2018
  16. segfaultmedaddy commented on Sep 9, 2018

    @segfaultmedaddy
    Contributor

    So now as Dart 2.0 is released would be there any changes so it finally get happened?

  17. munificent commented on Sep 10, 2018

    @munificent
    MemberAuthor

    Now all that's left is to design and implement non-nullable types, figure out a migration plan, add language features to make them more usable, etc. :) Basically, we have to do all the work. It's a giant feature.

  18. gbaldeck commented on Jan 23, 2019

    @gbaldeck

    If this year there was only one feature that dart shipped, this would be my choice. I really hope it becomes a high priority soon.

  19. mraleph commented on Jan 24, 2019

    @mraleph
    Member

    @gbaldeck and that's exactly what is happening this year see dart-lang/language#110 :)

    @munificent Should we close this and all associated issues and indicate that people can follow along the new language process in the language repo?

  20. munificent commented on Jan 24, 2019

    @munificent
    MemberAuthor

    Yeah, good call. Closing this in favor of dart-lang/language#110 which is the main tracking issue for the current plan to add non-nullable types.

  21. removed
    status-blockedBlocked from making progress by another (referenced) issue
    on Jan 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-metaCross-cutting, high-level issues (for tracking many other implementation issues, ...).

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions