inexhaustiveSwitch function
String
inexhaustiveSwitch(
{ - required SumTypeSpec spec,
- required bool implement,
})
Implementation
String inexhaustiveSwitch({
required SumTypeSpec spec,
required bool implement,
}) =>
function(
type: "\$T",
name: "iswitcho",
typeParams: [const TypeParamSpec(name: "\$T")],
namedParams: [
param(
type: "required \$T Function()",
name: "otherwise",
),
for (final caseSpec in spec.cases)
param(
type: [
"\$T Function(",
if (caseSpec.type.requiresPayload) caseSpec.type.name,
")?",
].join(),
name: caseSpec.name,
),
],
body: implement
? [
"\$T _otherwise(Object? _) => otherwise();",
"return iswitch(",
...spec.cases.map((c) =>
"${c.name}: ${c.name} ?? ${c.type.requiresPayload ? "_otherwise" : "otherwise"},"),
");",
]
: null,
);