eredo / dartson

Dartson is a Dart library that can be used to convert Dart objects into a JSON string.
MIT License
79 stars 29 forks source link

TypeTransformer: Use generic incorrectly #8

Closed CarterLi closed 9 years ago

CarterLi commented 10 years ago
class DateTimeParser<T> extends TypeTransformer {
  T decode(dynamic value) {
    return DateTime.parse(value); // Warning "The return type 'DateTime' is not a 'T'"
  }

  dynamic encode(T value) {
    return value.toString();
  }
}

And I think it's better to write

registerTransformer(new DateTimeParser());

instead of

registerTransformer(new DateTimeParser<DateTime>());

which should be defined inside of class DateTimeParser

eredo commented 10 years ago

Yes I agree. The idea behind it was more likely to create one transformer for several types. However I never found an actual use case. What I'm thinking of is:

// first argument is the type that should be transformed
registerTransformer(DateTime, new DateTimeParser());
CarterLi commented 10 years ago

What about this change: https://github.com/CarterLi/dartson/commit/0a200dd791fa8c585350ff8334e0c82ef14ed133

eredo commented 9 years ago

This is resolved in version 0.2.0 which comes with several changes. Please read the updated readme file.