Wildcard for Generics

Wildcards are landing for variables but is there an equivalent in the pipe for generics?

Class component<T, U> {} and then I want to express that T will not be used in implementation but still respect parent signature so <_, U>

Right now I use Object. Is there another approach?

1 Like

A post was merged into an existing topic: About the Dart category

You can use _, but notice two things:

  1. 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> {} 
  1. 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().
1 Like

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]) {}
}

1 Like

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]) {}
}
2 Likes

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([_], [_]) :confused:

But Never is better than Object in this case, totally forgot about it. Thanks!