Skip to content

Redirecting factory constructors should allow generics #3320

Closed as not planned
@Zekfad

Description

@Zekfad

When redirecting non-generic class to generic implementation you always lose generic argument.
Example:

sealed class Size {
  const Size();
  const factory Size.static(int size) = StaticSize;
  /// possible
  const factory Size.dynamic(ValueElement element) = DynamicSize;
  /// desired
  const factory Size.dynamic2<T extends ValueElement>(T element) = DynamicSize<T>;

  /// possible as method, but it's not a constructor and __not const__
  static Size dynamic3<T extends ValueElement>(T element) => DynamicSize<T>(element);
  int get size;
}

final class StaticSize implements Size {
  const StaticSize(this.size);

  @override
  final int size;
}

final class DynamicSize<T extends ValueElement> implements Size {
  const DynamicSize(this.element);

  final T element;

  @override
  int get size => element.asInt;
}

About instantiation, you can't call constructor like const Size<T>.dynamic(value) because Size have no argument and that's ok, but you can do const Size.dynamic<T>(value) if it's static method, but not a constructor.

It would be nice to have an exception for redirecting constructors to have generics, because that would allow better usage of const factory.

Metadata

Metadata

Assignees

No one assigned

    Labels

    requestRequests to resolve a particular developer problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions