sum_types 0.2.1+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 84

Build Status sum_types version sum_types_generator version

sum_types and sum_types_generator #

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

NOTE: v0.2.0 is a major update with backward-incompatible changes.

Example #

In 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):

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

class Nat extends _$Nat {
  const : super(zero: const Unit());
  const value) : super(next: value);

  Nat operator +(Nat other) => this.iswitch(
        zero: () => other,
        next: (next) => + other),

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

Features #


  • [x] Const case-constructors (const
  • [x] Extensible sum-types (Nat.toInt())
  • [x] Nested sum-types
  • [x] Recursive sum-types (Case<_Nat>(name: "next")
  • [x] Generic sum-types (Either<Left, Right>)
  • [x] Exhaustive in-line iswitch
  • [x] Inexhaustive in-line iswitcho (with otherwise: case)


  • [x] No-payload cases (Case<void>(name: "zero")
  • [x] Default case-names (Case<String>()JSON.string("some"))


  • [x] Equality test
  • [x] Hash function
  • [x] To string conversion

Serialization-deserialization support through product-types interoperability:

  • [x] Deserialization support (NatRecord<Self>, Nat.load<T extends NatRecord<T>>(T rec))
  • [x] Serialization support (Nat.dump<T>(T Function({Unit zero, T next} make)))

Development #

Find the upcoming development plans in the project planner.

0.2.1+2 #

  • Make sure no dependency_overrides are used in the example project

0.2.1+1 #

  • Allow "analyzer" versions below 0.40.0

0.2.1 #

  • Fix load(·) method generation.

0.2.0+1 #

  • Fix inter-package dependencies.

0.2.0 #

  • This is a major update with backward-incompatible changes!
  • Implement a new generation scheme with user-defined top-level classes. The new scheme improves interoperability with other code generators and simplifies sum-type annotations a lot.

0.1.5+1 #

  • Update

0.1.5 #

  • Support generic sum-types.
  • Restructure the example project.

0.1.4 #

  • Allow nesting sum-types (by mentioning anchors).
  • Stop using dynamic and Null as empty-case markers, use only void.

0.1.3 #

  • Support serialization-deserialization of the generated sum-types through interoperability with user-defined product-types (XRecordBase<Self>, X.dump(·), X.load(·))

0.1.2+4 #

  • Update and the package description.

0.1.2+3 #

  • Attempt to not publish uncommited changes.

0.1.2+2 #

  • Extend
  • Include analysis_options.yaml.

0.1.2+1 #

  • Relax dependency constraints to improve compatibility.

0.1.2 #

  • Integrate an example into the package.

0.1.1 #

  • Include a change-log file.

0.1.0 #

  • Define the annotations for sum_types_generator.
  • Include Unit type, required by the generator.


Example #

A sum_types and sum_types_generator packages usage example.

Running #

With Dart SDK #

As usual, run pub get first, then pub run example:main.

With Flutter SDK #

As usual, run flutter pub get first, then flutter pub run example:main.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  sum_types: ^0.2.1+2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:sum_types/sum_types.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.4.0 <3.0.0
meta ^1.1.0 1.1.8
Dev dependencies
dependency_validator any