dart_mappable icon indicating copy to clipboard operation
dart_mappable copied to clipboard

Mapper for Generic class with Type bounds.

Open LeoBound opened this issue 5 months ago • 0 comments

I want to create a SimpleMapper for my generic class, but the type parameter of this class is bounded. For example (modifying the GenericBox<T> example):

class NumberBox<T extends num> {
  NumberBox(this.content);

  final T content;
}

 class NumberBoxMapper extends SimpleMapper1<NumberBox> {
  const NumberBoxMapper();

  @override
  NumberBox<T> decode<T>(dynamic value) {
    T content = MapperContainer.globals.fromValue<T>(value);
    return NumberBox<T>(content);
  }

  @override
  dynamic encode<T>(NumberBox<T> self) {
    return MapperContainer.globals.toValue<T>(self.content);
  }

  @override
  Function get typeFactory => <T>(f) => f<NumberBox<T>>();
}  

is invalid as the type bounds of encode and decode are unrestricted.

Is there a way around this, I was hoping I could make NumberBoxMapper generic e.g. NumberBoxMapper<T extends num>

Thanks 🙂

LeoBound avatar Sep 03 '24 12:09 LeoBound