expandUserVariants function

List<List<String>> expandUserVariants(
  1. Iterable<TypeArgVariantSpec> specs,
  2. Iterable<String> candidates
)

Expand a list of variant specs against a candidate pool, de-duplicating the resulting tuples while preserving first-seen order.

Throws ArgumentError when the specs do not all share the same arity (a generic base has a fixed number of type parameters, so every variant for it must declare the same number of slots).

Implementation

List<List<String>> expandUserVariants(
  Iterable<TypeArgVariantSpec> specs,
  Iterable<String> candidates,
) {
  final specList = specs.toList();
  if (specList.isNotEmpty) {
    final arity = specList.first.arity;
    for (final spec in specList) {
      if (spec.arity != arity) {
        throw ArgumentError(
          'All variants for a generic base must share the same arity; '
          'expected $arity, found ${spec.arity} in $spec',
        );
      }
    }
  }
  final seen = <String>{};
  final out = <List<String>>[];
  for (final spec in specList) {
    for (final tuple in spec.expand(candidates)) {
      if (seen.add(tuple.join(''))) out.add(tuple);
    }
  }
  return out;
}