Skip to content

Generics seem too complicated as defined #11

@TekExplorer

Description

@TekExplorer

The generics side of things is honestly a bit strange to me.

I don't understand why we have positional parameters, and frankly i'm not sure why we're keeping a dynamic codable in the first place.

Why not:

class Box<T> implements SelfEncodable {
  Box(this.label, this.data);

  final String label;
  final T data;

  // static const Codable1<Box, dynamic> codable = BoxCodable();
  static Codable<Box<T>> codable<T>({Codable<T>? usingT}) => BoxCodable<T>(usingT: usingT);
  // alternatively, if we WANT the dynamic codable:
  static const codable = BoxCodable<dynamic>(); // + the extension below
  // BoxCodable.codable<Person>(usingT: Person.codable);

  @override
  void encode(Encoder encoder, {Encodable<T>? usingT}) {
    encoder.encodeKeyed()
      ..encodeString('label', label)
      ..encodeObject('data', data, using: usingT)
      ..end();
  }
}
class BoxCodable<T> extends Codable<Box<T>> {
  const BoxCodable({this.usingT});
  final Codable<T>? usingT;

  @override
  void encode(Box<T> value, Encoder encoder) {
    value.encode(encoder, usingT: usingT);
  }

  @override
  Box<T> decode(Decoder decoder) {
    final mapped = decoder.decodeMapped();
    return Box(
      mapped.decodeString('label'),
      mapped.decodeObject('data', using: usingT),
    );
  }
}

final Codable<Box<String>> boxStringCodable = Box.codable<String>();

// possibly
extension BoxCodableT on BoxCodable<dynamic> {
  Codable<Box<T>> call<T>({Codable<T>? usingT}) => BoxCodable(usingT: usingT);
}

This simplifies the weirdness with "use" and actually feels like a generic.

To be fair, I don't fully understand exactly what's happening, but it feels weirdly complicated for basic generics

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions