First, T and U are just strings. They are NOT type. So <Void, U> is not the Void type. Nor <_, U> is “ignored”. You just named a generic type as _. This is a name. You can name it String if you want. It will NOT be the String type.
Those are valid:
abstract interface class IContract<A, B> {}
final class ConcreteA<Void, B> implements IContract<String, B> {}
final class ConcreteB<_, B> implements IContract<_, B> {}
Those types are fake. They exist only in compile type. Dart will delete them when compiling. Generics in dart are very weak, almost unusable. You can’t do what you want, for instance. In C#, you could just omit the type (in both declaration, such as <,U> or by inference, by just using the type without specifying the generics). That’s the reason we can’t do nice things, such as ignoring it or using something like T.new().
Maybe I’ll open a ticket if I can clarify the requirement enough.
I went further with your example but what I request is really in the title.
A symbols that would represent a fully ignorable variable AND type, even if the optional [?]
Notice that ConcreteB.access is read as private field, which is expected but I would like
to mark a fully ignorable instead of [Object? a] => B access(_)
abstract class IContract<A, B> {
B access([A a]);
}
final class ConcreteA<Void, B> implements IContract<String, int> {
@override
int access([String? a]) { }
}
final class ConcreteB<B> implements IContract<_, B> {
@override
B access([_? a]) {}
}
I think in general when you do not want to specify type parameter, you should not – because why then. If you want to “omit” one type argument, you can use Never.
I don’t understand why you dont wanna specify the type argument when you are forced to implement function that uses it. But in that case probably using Never would also work?
final class ConcreteC<B> implements IContract<Never, B> {
@override
B access([Never? a]) {}
}
I want to mark the reader that the implementation does not need let’s say an input. It will respect the contract but may get the info from maybe a timer or a preconfigured source.
Some langs it’s common to use _ or ? to mark to type of acceptance and it’s aligned with method and generics.
For the same reason you can have to declare method(_, _) and it’s more readable than method(Never? a, Never?b). Well maybe in dart method([_], [_])
But Never is better than Object in this case, totally forgot about it. Thanks!
Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC.
Using contents of this forum for the purposes of training proprietary AI models is forbidden. Only if your AI model is free & open source, go ahead and scrape.