union_type 1.0.1 copy "union_type: ^1.0.1" to clipboard
union_type: ^1.0.1 copied to clipboard

Dart union type library.

union_type #

version 1.0.0+1 build status

Dart union type library. This library no longer depends on dart:mirrors.

Usage #

union_type provides helpers for type assertions across types that do not share ancestry.

import 'package:union_type/union_type.dart';
const UnionType BAZ = const UnionType('Baz', types: const [Foo, Bar]);

main() {
    BAZ.enforce(new Foo()); // No error
    BAZ.enforce(new Bar()); // No error
    BAZ.enforce({}); // TypeError
    BAZ.enforceAll([1, 2]); // TypeError
    
    if (BAZ.check(foo)) {
        // Do something
    } else {
        // Do something else
    }
}

Union types can assert other union types as well.

const UnionType MASTER =
    const UnionType('MasterType', types: const [BAZ, OtherType]);

You can also create a list of items, asserting that they match the type.

import 'package:union_type/union_type.dart';
const UnionType MONSTER =
    const UnionType('Monster', types: const [Franken, Stein]);

main() {
    var monsterArmy = new TypedList(MONSTER);

    monsterArmy.add(new Franken());
    monsterArmy.add(new Stein());

    for (var monster in monsterArmy) {
        print('MWAHAHAHA: $monster');
    }

    monsterArmy.add(2); // TypeError
    monsterArmy.addAll([2, 3, 4, '5']); // TypeError
}
0
likes
25
pub points
0%
popularity

Publisher

unverified uploader

Dart union type library.

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

matcher

More

Packages that depend on union_type