Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/werediver/sum_types.dart
A code generator enabling sum-types in Dart
https://github.com/werediver/sum_types.dart
algebraic-data-types code-generation dart flutter sum-types
Last synced: 29 days ago
JSON representation
A code generator enabling sum-types in Dart
- Host: GitHub
- URL: https://github.com/werediver/sum_types.dart
- Owner: werediver
- License: mit
- Created: 2019-08-19T19:43:49.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-28T13:17:36.000Z (almost 2 years ago)
- Last Synced: 2024-09-27T17:01:39.143Z (about 1 month ago)
- Topics: algebraic-data-types, code-generation, dart, flutter, sum-types
- Language: Dart
- Size: 121 KB
- Stars: 40
- Watchers: 2
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.com/werediver/sum_types.dart.svg?branch=master)](https://travis-ci.com/werediver/sum_types.dart)
[![sum_types version](https://img.shields.io/pub/v/sum_types?label=sum_types)](https://pub.dev/packages/sum_types)
[![sum_types_generator version](https://img.shields.io/pub/v/sum_types_generator?label=sum_types_generator)](https://pub.dev/packages/sum_types_generator)## sum_types and sum_types_generator
[sum_types](https://pub.dev/packages/sum_types) and [sum_types_generator](https://pub.dev/packages/sum_types_generator) packages together define a code generator enabling [sum-types](https://en.wikipedia.org/wiki/Sum_type) in Dart.
NOTE: v0.2.0 is a major update with backward-incompatible [changes](CHANGELOG.md#020).
## Example
In [example/lib/src/](example/lib/src) you can find a few sum-type declarations and the code generated for them.
This one models the natural numbers (with zero):
```dart
import 'package:meta/meta.dart';
import 'package:sum_types/sum_types.dart';@SumType()
class Nat extends _$Nat {
const Nat.zero() : super(zero: const Unit());
const Nat.next(Nat value) : super(next: value);Nat operator +(Nat other) => this.iswitch(
zero: () => other,
next: (next) => Nat.next(next + other),
);int toInt() => this.iswitch(
zero: () => 0,
next: (next) => 1 + next.toInt(),
);
}
```## Features
Core:
- [x] Const case-constructors (`const Nat.zero()`)
- [x] Extensible sum-types (`Nat.toInt()`)
- [x] Nested sum-types
- [x] Recursive sum-types (`Case<_Nat>(name: "next")` → `Nat.next(Nat.zero())`)
- [x] Generic sum-types (`Either`)
- [x] Exhaustive in-line `iswitch`
- [x] Inexhaustive in-line `iswitcho` (with `otherwise:` case)Sugar:
- [x] No-payload cases (`Case(name: "zero")` → `Nat.zero()`)
- [x] Default case-names (`Case()` → `JSON.string("some")`)Trivia:
- [x] Equality test
- [x] Hash function
- [x] To string conversionSerialization-deserialization support through product-types interoperability:
- [x] Deserialization support (`NatRecord`, `Nat.load>(T rec)`)
- [x] Serialization support (`Nat.dump(T Function({Unit zero, T next} make))`)## Development
Find the upcoming development plans in the [project planner](https://github.com/werediver/sum_types.dart/projects/1).