match 0.4.1 copy "match: ^0.4.1" to clipboard
match: ^0.4.1 copied to clipboard

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

example/lib/main.dart

import 'package:match/match.dart';

part 'main.g.dart';

@match
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.match(
    value: (v) => v.value,
    add: (a) => eval(a.e1) + eval(a.e2),
  );
}

@match
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.match(
    red: () => 1,
    green: () => 2,
    blue: () => 3,
  ));

  print(r.matchAny(
    red: () => 1,
    any: () => 3,
  ));
}
24
likes
150
points
147
downloads

Documentation

API reference

Publisher

verified publisherbech.io

Weekly Downloads

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

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

collection

More

Packages that depend on match