join<T> method
T
join<T>(
- T first(
- A value
- T second(
- B value
- T third(
- C value
- T forth(
- D value
- T fifth(
- E value
- T sixth(
- F value
Transform all the potential types that value
can take into a single
unique type.
For example, we can use join to convert a Union2<String, int>
into
an int:
Union2<String, int> union2;
int result = union2.join(
(value) => int.tryParse(value) ?? 0,
(value) => value,
);
Alternatively, join can return unions too, for more advanced cases that map cannot handle.
For example, we could use join to transform Union2<String, int>
into
Union2<int, FormatException>
, to have a clean error handling on parse
errors:
Union2<String, int> union2;
Union2<int, FormatException> res = union2.join(
(value) {
final parsed = int.tryParse(value);
return parsed != null
? parsed.asFirst()
: FormatException().asSecond();
},
(value) => value.asFirst(),
);
Implementation
// ignore: missing_return, the switch always returns
T join<T>(
T first(A value),
T second(B value),
T third(C value),
T forth(D value),
T fifth(E value),
T sixth(F value),
) {
late T res;
this(
(a) => res = first(a),
(a) => res = second(a),
(a) => res = third(a),
(a) => res = forth(a),
(a) => res = fifth(a),
(a) => res = sixth(a),
_noop,
_noop,
_noop,
);
return res;
}