Skip to content

Instantly share code, notes, and snippets.

@micimize
Created January 8, 2020 14:00
Show Gist options
  • Save micimize/39f6d59492b3c6792e607a476fb131a6 to your computer and use it in GitHub Desktop.
Save micimize/39f6d59492b3c6792e607a476fb131a6 to your computer and use it in GitHub Desktop.
type ideas in dart
/// Extensible enum that has `instance.asKnown` for good switch tooling
enum KnownReleaseType {
ALPHA,
BETA,
GAMMA,
}
class ReleaseType {
ReleaseType(String value) : value = _parseValue(value);
final Object value;
static const ALPHA = KnownReleaseType.ALPHA;
static const BETA = KnownReleaseType.BETA;
static const GAMMA = KnownReleaseType.GAMMA;
KnownReleaseType get asKnown => value is KnownReleaseType ? value : null;
static Object _parseValue(String value) {
switch (value) {
case 'ALPHA':
return KnownReleaseType.ALPHA;
case 'BETA':
return KnownReleaseType.BETA;
case 'GAMMA':
return KnownReleaseType.GAMMA;
}
return value;
}
@override
int get hashCode => value.hashCode;
@override
bool operator ==(Object o) => o is ReleaseType && o.value == value;
}
@micimize
Copy link
Author

I bet enum + extension can be hacked into variants

enum E { foo, biz, bar }

extension F on E {
  E shift(){
    switch(this){
      case E.foo:
        return E.biz;
       case E.biz:
        return E.bar;
      case E.bar:
        return E.foo;
     }
    throw Error();
  }
}


void main() {
  print(E.foo.shift().shift());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment