Entilore
Entilore

Reputation: 170

Wrap a generic class?

I'm trying to create a Sentry instrumenter for a gRPC server, which is basically generated code feeding a map with handler. I'm trying to wrap the handler, which I'm struggling on right now, because the handler is stored as a function in a generic class. Here is a simplification of the structure:

abstract class ServiceMethod<Q> {
  final Function(Q) action;

  ServiceMethod(this.action);
}

class MyServiceMethod<Q> extends ServiceMethod<Q> {
  MyServiceMethod(super.action);
}

class Service {
  final Map<String, ServiceMethod> map;

  Service(this.map);

  ServiceMethod? lookup(String name) {
    return map[name];
  }
}

There are several services, I could modify them, but I'd rather avoid it as I could have a lot of them. What I can do however is to wrap those services, so that the lookup method returns another implementation of ServiceMethod:

class WrappingContainer extends Service {
  WrappingContainer(super.map);


  ServiceMethod? lookup(String name) {
    final lookedUp = super.lookup(name);

    if (lookedUp == null) return lookedUp;

    return Wrapper<dynamic>(lookedUp);
  }
}

class Wrapper<Q> extends MyServiceMethod<Q> {
  Wrapper(ServiceMethod<Q> base) : super(base.action);
}

This is failing, with the following error:

// type '(List<int>) => void' is not a subtype of type '(dynamic) => dynamic'


main() {
  final base = MyServiceMethod((List<int> list) => print(list.join('-')));

  WrappingContainer({'base': base}).lookup('base')?.action([1]);
}

Any idea on how I could solve it? I've tried with mixins and extensions but I'm always reaching the same problem...

Upvotes: 0

Views: 29

Answers (0)

Related Questions