when_extension 0.1.2 copy "when_extension: ^0.1.2" to clipboard
when_extension: ^0.1.2 copied to clipboard

discontinued

Dart library with when annotation for generating custom when extensions and extension methods for dart builtin types

example/lib/main.dart

import 'package:when_extension/when_extension.dart';

part 'main.g.dart';

@when
abstract class Expr {}

class Value implements Expr {
  int value;
  Value({this.value});
}

class Add implements Expr {
  Expr e1;
  Expr e2;
  Add({this.e1, this.e2});
}

int eval(Expr expr) {
  return expr.when(
    value: (v) => v.value,
    add: (a) => eval(a.e1) + eval(a.e2),
  );
}

@when
enum Color {
  red,
  green,
  blue,
}

void main(List<String> arguments) {
  final e = Add(
    e1: Add(e1: Value(value: 10), e2: Value(value: 20)),
    e2: Value(value: 20),
  );
  print(eval(e));

  final r = Color.red;
  print(r.when(
    red: () => 1,
    green: () => 2,
    any: () => 3,
  ));
}
0
likes
30
pub points
0%
popularity

Publisher

verified publisherbech.io

Dart library with when annotation for generating custom when extensions and extension methods for dart builtin types

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

More

Packages that depend on when_extension