-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Can't use type arguments with statics and constants #25572
Comments
Right. There are a couple of limitations in play here:
If you don't need to use const here, you could address this with a generic method: class Foo<T> {
const Foo();
static Foo<S> bar<S>() => const Foo<S>();
}
Foo.bar<int>(); // Note the <int> is after the method, not the class. However, the VM does not currently reify type arguments for generic methods, so this won't actually let you use T at runtime. |
Yup! I just wanted to note down some context. For what it's worth, I agree with you, though putting type annotations in scope inside const expressions adds a surprising amount of complexity. |
I bet. |
See also: #26291. I'll leave this one open too since that one is restricted to const which this issue also talks about static members. |
The These days you can just write |
The fact that type parameters are non-const (while I'd expect the compiler to type-check / infer every parametrization statically) makes it more difficult to refer to type parameters in annotations (and thus in code generators). For instance, I'm working on a sum-types [-alike classes] generator, sum_types_generator, which would benefit from annotations being able to refer to type parameters of an annotated declaration to generate generic sum-types: @SumType([
@Case<T>(name: "some"), // Not possible, because `T` is not a const-expression
@Case<void>(name: "none")
])
mixin _Optional<T> implements _OptionalBase {} (one can think of generating This still can be worked around with a much less nice |
This code doesn't compile:
This prevents us from having statics that walk a tree looking for an instance of a specific instance (e.g. walking a tree looking for a
Foo<int>
).The text was updated successfully, but these errors were encountered: