-
Notifications
You must be signed in to change notification settings - Fork 199
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
Allow type-checking type variables #459
Comments
If we made the |
Making This happens fairly often with annotation processors in Java, and I think it would be nice to have a similar feature in Dart. Speaking of similarities to Java, I'd like to add something to the |
One issue with making If you can do |
We could also add a lint to prevent this case. |
However |
@lrhn This would not be a problem if |
Of course, we might as well mention that the test which is the topic of this issue can be expressed today, if we're willing to pay for an allocation of a list (or we could use some other generic class, if that's cheaper): void foo<X>() {
if (<X>[] is List<int>) {
// Here it is guaranteed that `X <: int`.
}
}
void main() {
foo<String>();
} But we should definitely try to find the time to get support for a generic |
I would assume that an instance of If we actually have a It's also a detour and a waste of effort to turn a type parameter into a (Or we could decide that |
Surely, In other words, if we make
Agreed, but nothing stops a compiler from compiling |
Pulling from #1971, given these: class Constraint { int get temp => 0; }
void constrained<T extends Constraint>(T arg) {} these two variations seem like they should work: void foo1<T>(T arg) {
if(T is Constraint) { // always false because T is a Type
print(arg.temp);
constrained<T>(arg);
}
}
void foo2<T>(T arg) {
if(arg is Constraint) {
print(arg.temp); /// valid, since `arg` is promoted to `Constraint`
barConstrained<T>(arg); /// error, since `T` is NOT promoted to `T extends Constraint`
}
} Seems this issue is about |
We wouldn't be able to (soundly) promote So we'd really need the ability to express directly that we want to test and promote a type variable, and that's what this issue is about. |
Is it possible to check if a generic type implements an interface during runtime? abstract class MyInterface {
void someCapability();
}
class MyConcreteType implements MyInterface {
@override
void someCapability() {
throw UnimplementedError('Coming soon!');
}
}
void MyFlexibleFunction<T>(T param) {
// This condition does not currently exist or compile successfully
// But conveys what I am trying to achieve
if (T implements MyInterface) {
final MyInterface myInterface = param as MyInterface;
myInterface.someCapability();
}
} |
This proposal aside, this is currently possible with if (param is MyInterface) |
I often see people trying to check the type of a type variable by doing
if (T is int) ...
.This doesn't work, the expression
T
evaluates to aType
object which is not of typeint
.The idea has merit, though.
We could perhaps special case
typeVariable is type
to actually check whether the type oftypeVariable
is a subtype oftype
. The expression is useless otherwise, so it's not like we will break any reasonable program, and users obviously find the syntax intuitive, since they keep using it.The text was updated successfully, but these errors were encountered: