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

Nullable types are not assinable to a variable. #1553

Closed
Reductions opened this issue Mar 29, 2021 · 7 comments
Closed

Nullable types are not assinable to a variable. #1553

Reductions opened this issue Mar 29, 2021 · 7 comments
Labels
state-duplicate This issue or pull request already exists

Comments

@Reductions
Copy link

Reductions commented Mar 29, 2021

At the moment this is not posible:

void main() {
  Type t = int?; // I also tried to do: Type t = (int?);
  print(t);
}

Here is the error I get:

test_dart.dart:2:16: Error: Expected an identifier, but got ';'.
Try inserting an identifier before ';'.
  Type t = int?;
               ^
test_dart.dart:2:16: Error: Expected ':' before this.
  Type t = int?;
               ^
test_dart.dart:2:12: Error: A value of type 'Type' can't be assigned to a variable of type 'bool'.
 - 'Type' is from 'dart:core'.
  Type t = int?;
           ^
@eernstg
Copy link
Member

eernstg commented Mar 29, 2021

Use this:

Type typeOf<X>() => X;

void main() {
  Type t = typeOf<int?>();
  print(t);
}

Cf. #123, I'll close this as a duplicate.

@eernstg eernstg added the state-duplicate This issue or pull request already exists label Mar 29, 2021
@Levi-Lesches
Copy link

Would the new typedefs help with this?

typedef MaybeInt = int?

void main() {
  Type t = MaybeInt;
  print(t);
}

@Reductions
Copy link
Author

@Levi-Lesches You can not typedef anything but functional types.

@Levi-Lesches
Copy link

That's being changed in #65, and hopefully releasing soon. Hopefully that should fix this pattern.

@eernstg
Copy link
Member

eernstg commented Apr 6, 2021

It is indeed possible to use the new and more general kind of type alias (currently needing --enable-experiment=nonfunction-type-aliases, soon to be enabled by default):

typedef MaybeInt = int?;

However, I'd still recommend using something like typeOf<int?>() when possible, because the type alias adds MaybeInt to the global namespace, and that would typically not be helpful.

@Reductions
Copy link
Author

Reductions commented Apr 6, 2021

Glad to hear that typedef of nonfunctional types is coming to dart. It will be beneficial when working with nested maps and lists.
@eernstg

@eernstg
Copy link
Member

eernstg commented Apr 7, 2021

Thanks, @Reductions!

I'll close this issue: It may be seen as a proposal to allow T? as an expression when T is a type, but that's already on the agenda in #123 (comment), hence the 'state-duplicate' label.

@eernstg eernstg closed this as completed Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants