sum_types_generator 0.2.1+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 80

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';

@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<Left, Right>)
  • [x] Exhaustive in-line iswitch
  • [x] Inexhaustive in-line iswitcho (with otherwise: case)

Sugar:

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

Trivia:

  • [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 README.md.

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 README.md and the package description.

0.1.2+3 #

  • Attempt to not publish uncommited changes.

0.1.2+2 #

  • Extend README.md.
  • 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/README.md

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:


dependencies:
  sum_types_generator: ^0.2.1+2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support 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_generator/sum_types_generator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
60
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
80
Learn more about scoring.

We analyzed this package on Dec 9, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.13.1+4

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.4.0 <3.0.0
analyzer >=0.36.4 <0.40.0 0.39.2+1
build ^1.1.0 1.2.2
meta ^1.1.0 1.1.8
source_gen ^0.9.2 0.9.4+6
sum_types ^0.2.0 0.2.1+2
Transitive dependencies
_fe_analyzer_shared 1.0.1
args 1.5.2
async 2.4.0
charcode 1.1.2
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
dart_style 1.3.3
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
logging 0.11.3+2
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
path 1.6.4
pedantic 1.9.0
pub_semver 1.4.2
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+13
yaml 2.2.0
Dev dependencies
dependency_validator any