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

Strong null aware and ignorable type #2629

Closed
elbeicktalat opened this issue Nov 12, 2022 · 2 comments
Closed

Strong null aware and ignorable type #2629

elbeicktalat opened this issue Nov 12, 2022 · 2 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@elbeicktalat
Copy link

The problem

Dart has nice null safety support with it's awesome operators, null check ! and null aware ?.

The actual null aware is nice, but I was thinking about having a short syntax to deal with nullable variables in some better way. See the following example and how we can reduce the cost of typing multiple if statements. I have found this code in the flutter repo, there is a lot like this.

Actually:

  @override
  Iterable<RenderBox> get children {
    return <RenderBox>[
      if (leading != null)
        leading!,
      if (title != null)
        title!,
      if (subtitle != null)
        subtitle!,
      if (trailing != null)
        trailing!,
    ];
  }

Proportional

Introduce a ?! operator, this new operator converts nullable variables to non-nullable only if they're not null. Nullable variables which is not assigned will be ignored. I'm proposing to introduce a new ignorable variables concept, ignorable variable will be skipped by the compiler if it holds a null value. I'm not sure if we can introduce this new operator without intruding the ignorable type.

The same example of before, but with the new ?! operator:

  @override
  Iterable<RenderBox> get children {
    return <RenderBox>[
        leading?!,
        title?!,
        subtitle?!,
        trailing?!,
    ];
  }

Out of list context:

String name = "Olivier";
String? _lastName;

// Concatenate _lastName if it's not null, otherwise return only name.
// It's sounds like having a default value, but it's not. We simply ignored the _lastName variable. 
String get fullName => name + _lastName?!;

// Make the compiler ignoring the lastName getter and it's usages. 
String get lastName => _lastName?!;

Ignorable deceleration:

String?! name; // hold null or special value to indicate that this variable is ignorable, may `ignored` or `undefined`

// never will be called. 
print(name);

flutter context random examples:

// ignorable 
String?! name;

Text(name);

// actually
String? name;
name != null ? Text(name!) : null;

// null to ignorable
Text(name?!);

Conclusion

If we're able to deal with nullables as ignorables by only using the ?! operator it'll awesome since we'll have no breaking changes. I hope I was clear, Thanks for your time, special thanks to Dart team.

Good coding!

@elbeicktalat elbeicktalat added the feature Proposed language feature that solves one or more problems label Nov 12, 2022
@lrhn
Copy link
Member

lrhn commented Nov 12, 2022

The only place where I can see this working is in a collection literal, where it makes sense to omit a value. It doesn't work in general expressions, because they must have a value.
We cannot simply ignore an expression. If you do name + _lastName?!, and ignore the _lastName expression, the + is still there and needs an argument.
The Text(name) example also only works if the parameter of Text is optional, otherwise there is no value to pass to the constructor.

If it's just collection elements, then #323 should solve the same problem.

@elbeicktalat
Copy link
Author

@lrhn Thanks for the answer,
We stick with #323 issue.

Thanks!

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
Projects
None yet
Development

No branches or pull requests

2 participants