Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generics complicate self-referential class structure #1074

Closed
craiglabenz opened this issue Apr 21, 2024 · 2 comments
Closed

Generics complicate self-referential class structure #1074

craiglabenz opened this issue Apr 21, 2024 · 2 comments
Assignees
Labels
bug Something isn't working needs triage

Comments

@craiglabenz
Copy link

craiglabenz commented Apr 21, 2024

The following code structure builds correctly, but lacks ideal usage of generics.

class Base<T> {
  const Base();
}

// Ideally, this would be `BaseConverter<T>` and `Base<T>`, but doing so breaks the build
class BaseConverter extends JsonConverter<Base, Json> {
  const BaseConverter();

  // Assume working implementations here :)
  @override
  Json toJson(Base obj) => throw UnimplementedError();
  @override
  Base fromJson(Json json) => throw UnimplementedError();
}

@Freezed(genericArgumentFactories: true)
class Data<T> extends Base<T> with _$Data<T> {
  const Data._();

  // Ideally, this would be `@BaseConverter<T>()` and `Base<T>`, but doing so breaks the build
  const factory Data(@BaseConverter() Base child) = _Data;

  factory Data.fromJson(Json json, T Function(Object?) fromJsonT) =>
      _$DataFromJson(json, fromJsonT);
}
@rrousselGit
Copy link
Owner

Hello Craig!

This is a json_serializable issue, not a Freezed one. Using a plain class leads to the same error. I'd suggest opening an issue on json_serializable and asking Kevin about it :)

@craiglabenz
Copy link
Author

Thanks for the context, @rrousselGit :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

2 participants