generics method
Implementation
Iterable<String> generics(DartType k) {
final kc = k.element;
if (k is! ParameterizedType ||
kc is! ClassElement ||
kc.typeParameters.isEmpty) {
return [];
}
// maybe has type parameter type
if (kc.fields
.where((f) => visit(f.type, (x) {
return x is TypeParameterType;
}, (x, xs) => x || xs.reduce((a, b) => a || b)))
.isEmpty) {
return [];
}
final ks = k.typeArguments
.map((x) => x.element?.displayName ?? 'dynamic')
.toList();
final cs = kc.typeParameters.map((x) => x.name).toList();
final dict = <String, String>{};
for (var idx = 0; idx < cs.length; idx++) {
if (ks.length > idx) {
dict[cs[idx]] = ks[idx];
}
}
// redefine
final kxx = display(k);
final inits = <String>[];
inits.add('ark.constructor<$kxx>(() => $kxx());');
inits.addAll(kc.fields.mapIndexed((idx, f) {
final m = annotation(Index, e: f);
final i = index(idx + 1, f.name, f.type, m, refers: dict);
final a = accessor(display(k, refers: dict)!, i, f.name);
return 'ark.declare<$kxx>(\'${f.name}\',$a);';
}));
return inits;
}