schultek / dart_mappable

Improved json serialization and data classes with full support for generics, inheritance, customization and more.
https://pub.dev/packages/dart_mappable
MIT License
164 stars 23 forks source link

Mapper for Generic class with Type bounds. #228

Closed LeoBound closed 1 month ago

LeoBound commented 2 months ago

I want to create a SimpleMapper for my generic class, but the type parameter of this class is bounded. For example (modifying the GenericBox 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 🙂

schultek commented 1 month ago

I will add a SimpleMapper1Bounded for this.

schultek commented 1 month ago

Added in v4.3.0