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 null-check With Creating a New Variable #1526

Closed
EmreSURK opened this issue Mar 16, 2021 · 1 comment
Closed

Add null-check With Creating a New Variable #1526

EmreSURK opened this issue Mar 16, 2021 · 1 comment
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists

Comments

@EmreSURK
Copy link

EmreSURK commented Mar 16, 2021

Dart is great, and null-safety is great too. It allows developers to eliminate null-related-crashes easily.

I strongly believe we can move forward with syntax.

Currently, developers still need to check the variable if null like that:

var someVariable = data?.someMember;
if ( someVariable != null ) {
     // code..
}

These checks are so similar for non-optional values, so most developers get confused and can not see the beauty of the optional types. Most of the time the someVariable variable is not being used outside of the if block.

I am suggesting null checking while defining a non-optional variable inside if block:

if ( var someVariable = data?.someMember ) {
    // we have someVariable variable, it has data and it is not optional anymore.
} else {
   // we don't have someVariable variable, data or someMember is null. 
}

// someVariable variable is not defined here.

I inspired by other languages, of course.

Let me know what you think.

Best.

@EmreSURK EmreSURK added the feature Proposed language feature that solves one or more problems label Mar 16, 2021
@eernstg eernstg added the state-duplicate This issue or pull request already exists label Mar 16, 2021
@eernstg
Copy link
Member

eernstg commented Mar 16, 2021

This idea has been considered elsewhere already: #1201, #1210, #1420, so you may wish to contribute to the discussions there. Also, you may be interested in the topic field-promotion in general.

I'll close this issue as a duplicate of those.

@eernstg eernstg closed this as completed Mar 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants