Dart union type library. This library no longer depends on dart:mirrors
.
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
}
Add this to your package's pubspec.yaml file:
dependencies:
union_type: "^1.0.1"
You can install packages from the command line:
with pub:
$ pub get
with Flutter:
$ flutter packages get
Alternatively, your editor might support pub get
or packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:union_type/union_type.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
1.0.1 | Jul 15, 2017 |
|
|
1.0.0+1 | Dec 17, 2016 |
|
|
1.0.0 | Dec 17, 2016 |
|
|
We analyzed this package on Apr 23, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
30 | / 100 |
Health:
Code health derived from static analysis.
[more]
|
98 | / 100 |
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
80 | / 100 |
Overall score:
Weighted score of the above.
[more]
|
60 |
Detected platforms: Flutter, web, other
No platform restriction found in primary library
package:union_type/union_type.dart
.
Maintain CHANGELOG.md
.
Changelog entries help clients to follow the progress in your code.
The description is too short.
Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.
Maintain an example.
Create a short demo in the
example/
directory to show how to use this package. Common file name patterns include:main.dart
,example.dart
or you could also useunion_type.dart
.
Fix analysis and formatting issues.
Analysis or formatting checks reported 2 hints.
Run
dartfmt
to formatlib/matchers.dart
.Run
dartfmt
to formatlib/union_type.dart
.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=1.19.0 | ||
matcher | ^0.12.0+2 | 0.12.2 | |
Transitive dependencies | |||
path | 1.5.1 | ||
stack_trace | 1.9.2 | ||
Dev dependencies | |||
test | ^0.12.17+3 |