-
Notifications
You must be signed in to change notification settings - Fork 213
Assignability of generic type Never
#3670
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
Comments
In a static analysis, it's completely valid to pass a value of type The only problem is that, in practice, it's not possible to create a value of type Thus, it seems it's working perfectly as expected. Ps. I tried your second code and it does not break at compile time, but at runtime. |
I am not sure what you are trying to achieve (I didn't read the entire code). In your second sample code, by using void you would get the static analysis to tell you that, obviously,
void main() {
final list = <void>[1, 'String'];
print('First value: ${list[0]}'); // Compile-time error: this expression has a type of 'void' so its value can't be used.
} |
What @mateusfccp says, Your problem isn't That means that That's not just about never. So this is all working as intended, and until Dart gets a way to make class type parameters contravariant, is just something to be careful around. |
Yes, #524 is the issue for variance annotations. |
@lrhn is it possible to check if a generic type is In my usecase dart is inferring generic types as ![]() Any suggestions on how to prevent this? |
I noticed that the
Never
type seems to be assignable to any other type when working with generics:I would expect calling
fun
withval
to error at compile time, sinceObj<Never>
cannot be assigned toObj<String>
.In my specific case I am using
Never
to express a "missing"/"not possible" value (reference), and this breaks at compile time in cases like below:Is this the correct/expected behaviour?
The text was updated successfully, but these errors were encountered: