项目作者: freedomlayer

项目描述 :
Dart Union class (sum type)
高级语言: Dart
项目地址: git://github.com/freedomlayer/built_union.dart.git
创建时间: 2020-01-24T11:36:47Z
项目社区:https://github.com/freedomlayer/built_union.dart

开源协议:BSD 3-Clause "New" or "Revised" License

下载


Built Union: sum-types for Dart

Build Status
built_union version
built_union_generator version

built_union.dart introduces Union values, also known as Tagged unions (C), Enums (Rust), Sum types (Haskell).

A Union value is type that has a few different variants.
At any given time, the Union value has a value of a certain single variant.

You want to use Built Union if you need to represent a type that means:
“Either this or that, but not both at the same time”.

Fully compatible with BuiltValue.

Created as part of the Offst project.

How to use?

Add to your pubspec.yaml file:

  1. dependencies:
  2. meta: ^1.1.0
  3. built_union: ^0.1.0
  4. # built_value: ^7.0.6
  5. dev_dependencies:
  6. built_union_generator: ^0.1.0
  7. # built_value_generator: ^7.0.8
  8. build_runner: ^1.0.0

Note that versions might differ from this example, so make sure to use the
correct versions by checking at pub.dev.

You will most likely need built_value and built_value_generator for the
creation of BuiltValues and serialization. If you don’t, you can omit those
dependencies.

Examples

Quick links:

Consider the following SimpleUnion definition:

  1. @BuiltUnion()
  2. class SimpleUnion extends _$SimpleUnion {
  3. static Serializer<SimpleUnion> get serializer => _$simpleUnionSerializer;
  4. SimpleUnion.empty() : super.empty();
  5. SimpleUnion.integer(int integer) : super.integer(integer);
  6. SimpleUnion.tuple(int tupleInt, String tupleString)
  7. : super.tuple(tupleInt, tupleString);
  8. SimpleUnion.string(String string) : super.string(string);
  9. SimpleUnion.builtList(BuiltList<int> builtList) : super.builtList(builtList);
  10. }

The definition above means that SimpleUnion always must be in one of a few states:

  • empty
  • integer
  • tuple
  • string
  • builtList

Some examples for instantiating SimpleUnion:

  1. final simpleUnionEmpty = SimpleUnion.empty();
  2. final simpleUnionInteger = SimpleUnion.integer(3);
  3. final simpleUnionTuple = SimpleUnion.tuple(4, 'four');
  4. final simpleUnionString = SimpleUnion.string('String');
  5. final simpleUnionBuiltList = SimpleUnion.builtList(BuiltList([1,2,3,4]));

Serialization

Built Union supports json serialization.
Example:

  1. final simpleUnions = [
  2. SimpleUnion.empty(),
  3. SimpleUnion.integer(3),
  4. SimpleUnion.tuple(4, 'four'),
  5. SimpleUnion.string('string'),
  6. SimpleUnion.builtList(BuiltList([1,2,3,4])),
  7. ];
  8. for (final simpleUnion in simpleUnions) {
  9. final serialized = serializersWithPlugin.serialize(simpleUnion, specifiedType: FullType(SimpleUnion));
  10. JsonEncoder encoder = new JsonEncoder.withIndent(' ');
  11. print(encoder.convert(serialized));
  12. final simpleUnion2 = serializersWithPlugin.deserialize(serialized, specifiedType: FullType(SimpleUnion));
  13. expect(simpleUnion, simpleUnion2);
  14. }

Resulting json:

  1. "empty"
  2. {
  3. "integer": 3
  4. }
  5. {
  6. "tuple": [
  7. 4,
  8. "four"
  9. ]
  10. }
  11. {
  12. "string": "string"
  13. }
  14. {
  15. "builtList": [
  16. 1,
  17. 2,
  18. 3,
  19. 4
  20. ]
  21. }

Unimplemented features

  • Generics

Hacking guide

  • Run tool/presubmit to format, build, analyze and test all packages.
  • To work locally, run tool/local_deps enable. This will make sure that
    dependencies point to your local copy, and not to a remote copy. To restore
    remote dependencies, run tool/local_deps disable.

Thanks

Special thanks to David Morgan for the
guidance during the development of this package.

See the original issue
that led to the creation of this package.