sum_types 0.1.4 copy "sum_types: ^0.1.4" to clipboard
sum_types: ^0.1.4 copied to clipboard

outdated

sum_types and sum_types_generator packages together define a code generator enabling sum-types in Dart.

example/lib/example.dart

import 'package:json_annotation/json_annotation.dart';
import 'package:meta/meta.dart';
import 'package:sum_types/sum_types.dart';

part 'example.g.dart';

@SumType([
  Case<_Nat>(name: "some"),
  Case<void>(name: "none"),
])
mixin _MaybeNat implements _MaybeNatBase {}

@SumType([
  Case<void>(name: "zero"),
  Case<_Nat>(name: "next"),
])
mixin _Nat implements _NatBase {
  Nat operator +(Nat other) => iswitch(
        zero: () => other,
        next: (next) => Nat.next(next + other),
      );

  int toInt() => iswitch(
        zero: () => 0,
        next: (next) => 1 + next.toInt(),
      );
}

@JsonSerializable(includeIfNull: false)
class NatRecord implements NatRecordBase<NatRecord> {
  const NatRecord({
    this.zero,
    this.next,
  });

  factory NatRecord.fromJson(Map<String, dynamic> json) =>
      _$NatRecordFromJson(json);

  Map<String, dynamic> toJson() => _$NatRecordToJson(this);

  @override
  final Unit zero;
  @override
  final NatRecord next;
}

@SumType([
  Case<Map<String, _JSON>>(name: "object"),
  Case<List<_JSON>>(name: "array"),
  Case<String>(),
  Case<double>(name: "number"),
  Case<bool>(name: "boolean"),
  Case<void>(name: "empty"),
])
mixin _JSON {}
13
likes
0
pub points
76%
popularity

Publisher

unverified uploader

sum_types and sum_types_generator packages together define a code generator enabling sum-types in Dart.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

meta

More

Packages that depend on sum_types