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

[NNBD] Provide a way to check if a type is nullable #1258

Closed
pedromassango opened this issue Oct 12, 2020 · 3 comments
Closed

[NNBD] Provide a way to check if a type is nullable #1258

pedromassango opened this issue Oct 12, 2020 · 3 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@pedromassango
Copy link

pedromassango commented Oct 12, 2020

I'm not sure if this currently exist but I didn't found anything by looking at the docs. Also not sure if here is the right place for this proposal!

In Kotlin we are able to check whether a type is nullable or not.
Wondering if currently there is a way of doing that and if not we can consider this as a proposal.

In Kotlin we can achieve this by using reflections:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect/-k-type/is-marked-nullable.html

We can have TypeMirror.isNullable or isMarkedNullable which is true if the type was declared with a question mark (nullable) or false otherwise.

@pedromassango pedromassango added the feature Proposed language feature that solves one or more problems label Oct 12, 2020
@leafpetersen
Copy link
Member

If I understand correctly, you're asking about reflective support in dart:mirrors for nullable types? Note that for non-reflective uses null is T works for this.

@pedromassango
Copy link
Author

null is T

@leafpetersen what if I want to check an actual property?


String? foo;

// somewhere in my code


if (foo.isNullable) {
  ...
}

I actually need this to build a linter related to nullable return types. See dart-lang/linter#2352.

Cc @kevmoo

@leafpetersen
Copy link
Member

Lints are generally written using the analyzer APIs, which is more like a compiler model than a code generation model. I'm not sure what you're looking for here, but I don't think this is a language feature request. Perhaps follow up in the lint issue?

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