Skip to content

Instantly share code, notes, and snippets.

@rrousselGit
Last active October 21, 2019 15:24
Show Gist options
  • Select an option

  • Save rrousselGit/6e8c7d64f9fa35803a67209b8a960718 to your computer and use it in GitHub Desktop.

Select an option

Save rrousselGit/6e8c7d64f9fa35803a67209b8a960718 to your computer and use it in GitHub Desktop.
type safe Unions
enum _Union {
first,
second,
third,
forth,
fifth,
sixth,
seventh,
eighth,
ninth,
}
class Union2<A, B> {
const Union2.first(A value)
: _value = value,
_type = _Union.first;
const Union2.second(B value)
: _value = value,
_type = _Union.second;
final _Union _type;
final Object _value;
T join<T>(
T first(A value),
T second(B value),
) {
switch (_type) {
case _Union.first:
return first(_value as A);
case _Union.second:
return second(_value as B);
default:
throw FallThroughError();
}
}
@override
bool operator ==(Object other) =>
other is Union2<A, B> && other._type == _type && other._value == _value;
@override
int get hashCode => _type.hashCode ^ _value.hashCode;
}
class Union3<A, B, C> {
const Union3.first(A value)
: _value = value,
_type = _Union.first;
const Union3.second(B value)
: _value = value,
_type = _Union.second;
const Union3.third(C value)
: _value = value,
_type = _Union.third;
final _Union _type;
final Object _value;
T join<T>(
T first(A value),
T second(B value),
T third(C value),
) {
switch (_type) {
case _Union.first:
return first(_value as A);
case _Union.second:
return second(_value as B);
case _Union.third:
return third(_value as C);
default:
throw FallThroughError();
}
}
@override
bool operator ==(Object other) =>
other is Union3<A, B, C> &&
other._type == _type &&
other._value == _value;
@override
int get hashCode => _type.hashCode ^ _value.hashCode;
}
class Union4<A, B, C, D> {
const Union4.first(A value)
: _value = value,
_type = _Union.first;
const Union4.second(B value)
: _value = value,
_type = _Union.second;
const Union4.third(C value)
: _value = value,
_type = _Union.third;
const Union4.forth(D value)
: _value = value,
_type = _Union.forth;
final _Union _type;
final Object _value;
T join<T>(
T first(A value),
T second(B value),
T third(C value),
T forth(D value),
) {
switch (_type) {
case _Union.first:
return first(_value as A);
case _Union.second:
return second(_value as B);
case _Union.third:
return third(_value as C);
case _Union.forth:
return forth(_value as D);
default:
throw FallThroughError();
}
}
@override
bool operator ==(Object other) =>
other is Union4<A, B, C, D> &&
other._type == _type &&
other._value == _value;
@override
int get hashCode => _type.hashCode ^ _value.hashCode;
}
class Union5<A, B, C, D, E> {
const Union5.first(A value)
: _value = value,
_type = _Union.first;
const Union5.second(B value)
: _value = value,
_type = _Union.second;
const Union5.third(C value)
: _value = value,
_type = _Union.third;
const Union5.forth(D value)
: _value = value,
_type = _Union.forth;
const Union5.fifth(E value)
: _value = value,
_type = _Union.fifth;
final _Union _type;
final Object _value;
T join<T>(
T first(A value),
T second(B value),
T third(C value),
T forth(D value),
T fifth(E value),
) {
switch (_type) {
case _Union.first:
return first(_value as A);
case _Union.second:
return second(_value as B);
case _Union.third:
return third(_value as C);
case _Union.forth:
return forth(_value as D);
case _Union.fifth:
return fifth(_value as E);
default:
throw FallThroughError();
}
}
@override
bool operator ==(Object other) =>
other is Union5<A, B, C, D, E> &&
other._type == _type &&
other._value == _value;
@override
int get hashCode => _type.hashCode ^ _value.hashCode;
}
class Union6<A, B, C, D, E, F> {
const Union6.first(A value)
: _value = value,
_type = _Union.first;
const Union6.second(B value)
: _value = value,
_type = _Union.second;
const Union6.third(C value)
: _value = value,
_type = _Union.third;
const Union6.forth(D value)
: _value = value,
_type = _Union.forth;
const Union6.fifth(E value)
: _value = value,
_type = _Union.fifth;
const Union6.sixth(F value)
: _value = value,
_type = _Union.sixth;
final _Union _type;
final Object _value;
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),
) {
switch (_type) {
case _Union.first:
return first(_value as A);
case _Union.second:
return second(_value as B);
case _Union.third:
return third(_value as C);
case _Union.forth:
return forth(_value as D);
case _Union.fifth:
return fifth(_value as E);
case _Union.sixth:
return sixth(_value as F);
default:
throw FallThroughError();
}
}
@override
bool operator ==(Object other) =>
other is Union6<A, B, C, D, E, F> &&
other._type == _type &&
other._value == _value;
@override
int get hashCode => _type.hashCode ^ _value.hashCode;
}
class Union7<A, B, C, D, E, F, G> {
const Union7.first(A value)
: _value = value,
_type = _Union.first;
const Union7.second(B value)
: _value = value,
_type = _Union.second;
const Union7.third(C value)
: _value = value,
_type = _Union.third;
const Union7.forth(D value)
: _value = value,
_type = _Union.forth;
const Union7.fifth(E value)
: _value = value,
_type = _Union.fifth;
const Union7.sixth(F value)
: _value = value,
_type = _Union.sixth;
const Union7.seventh(G value)
: _value = value,
_type = _Union.seventh;
final _Union _type;
final Object _value;
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),
T seventh(G value),
) {
switch (_type) {
case _Union.first:
return first(_value as A);
case _Union.second:
return second(_value as B);
case _Union.third:
return third(_value as C);
case _Union.forth:
return forth(_value as D);
case _Union.fifth:
return fifth(_value as E);
case _Union.sixth:
return sixth(_value as F);
case _Union.seventh:
return seventh(_value as G);
default:
throw FallThroughError();
}
}
@override
bool operator ==(Object other) =>
other is Union7<A, B, C, D, E, F, G> &&
other._type == _type &&
other._value == _value;
@override
int get hashCode => _type.hashCode ^ _value.hashCode;
}
class Union8<A, B, C, D, E, F, G, H> {
const Union8.first(A value)
: _value = value,
_type = _Union.first;
const Union8.second(B value)
: _value = value,
_type = _Union.second;
const Union8.third(C value)
: _value = value,
_type = _Union.third;
const Union8.forth(D value)
: _value = value,
_type = _Union.forth;
const Union8.fifth(E value)
: _value = value,
_type = _Union.fifth;
const Union8.sixth(F value)
: _value = value,
_type = _Union.sixth;
const Union8.seventh(G value)
: _value = value,
_type = _Union.seventh;
const Union8.eighth(H value)
: _value = value,
_type = _Union.eighth;
final _Union _type;
final Object _value;
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),
T seventh(G value),
T eighth(H value),
) {
switch (_type) {
case _Union.first:
return first(_value as A);
case _Union.second:
return second(_value as B);
case _Union.third:
return third(_value as C);
case _Union.forth:
return forth(_value as D);
case _Union.fifth:
return fifth(_value as E);
case _Union.sixth:
return sixth(_value as F);
case _Union.seventh:
return seventh(_value as G);
case _Union.eighth:
return eighth(_value as H);
default:
throw FallThroughError();
}
}
@override
bool operator ==(Object other) =>
other is Union8<A, B, C, D, E, F, G, H> &&
other._type == _type &&
other._value == _value;
@override
int get hashCode => _type.hashCode ^ _value.hashCode;
}
class Union9<A, B, C, D, E, F, G, H, I> {
const Union9.first(A value)
: _value = value,
_type = _Union.first;
const Union9.second(B value)
: _value = value,
_type = _Union.second;
const Union9.third(C value)
: _value = value,
_type = _Union.third;
const Union9.forth(D value)
: _value = value,
_type = _Union.forth;
const Union9.fifth(E value)
: _value = value,
_type = _Union.fifth;
const Union9.sixth(F value)
: _value = value,
_type = _Union.sixth;
const Union9.seventh(G value)
: _value = value,
_type = _Union.seventh;
const Union9.eighth(H value)
: _value = value,
_type = _Union.eighth;
const Union9.ninth(I value)
: _value = value,
_type = _Union.ninth;
final _Union _type;
final Object _value;
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),
T seventh(G value),
T eighth(H value),
T ninth(I value),
) {
switch (_type) {
case _Union.first:
return first(_value as A);
case _Union.second:
return second(_value as B);
case _Union.third:
return third(_value as C);
case _Union.forth:
return forth(_value as D);
case _Union.fifth:
return fifth(_value as E);
case _Union.sixth:
return sixth(_value as F);
case _Union.seventh:
return seventh(_value as G);
case _Union.eighth:
return eighth(_value as H);
case _Union.ninth:
return ninth(_value as I);
default:
throw FallThroughError();
}
}
@override
bool operator ==(Object other) =>
other is Union9<A, B, C, D, E, F, G, H, I> &&
other._type == _type &&
other._value == _value;
@override
int get hashCode => _type.hashCode ^ _value.hashCode;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment