Is this the correct way to declare a "generic class" that extends another "generic class" in dart? Note that the generic parameter has a type restriction.
// available types
class BaseType {}
class DerivedType extends BaseType {}
class BaseClass<Type extends BaseType> {
final Type prop;
BaseClass(this.prop) {
// can be either BaseType or DerivedType
print(prop);
}
}
class DerivedClass<Type extends BaseType> extends BaseClass<BaseType> {
DerivedClass(BaseType prop) : super(prop);
}
The above code works, but I'm not sure if I am using the correct syntax.
Although your code is correct I think you made a semantic mistake in the generic of DerivedClass
:
// available types
class BaseType {}
class DerivedType extends BaseType {}
class BaseClass<T extends BaseType> {
final T prop;
BaseClass(this.prop) {
// can be either BaseType or DerivedType
print(prop);
}
}
class DerivedClass<T extends BaseType> extends BaseClass<T /*not BaseType*/> {
DerivedClass(T /*not BaseType*/ prop) : super(prop);
}