Indexed by TopGit from live GitHub metadata: rrousselGit/union has 81 stars, written primarily in Dart. Union types for dart
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
A union is an object that holds a value which can be of different types,
and allow manipulating that value in a type-safe way.
Unions comes in different variants, where its name "Union" is suffixed
by a number (such as Union2 or Union3).
That number represents the number of different types the value stored
can take.
For example, if a value can be both a [String] and a [int], that makes
two different types. We can therefore use Union2 like so:
Union2<String, int> myUnion;
or:
Union2<int, String> myUnion;
Note that, due to language limitations, we cannot assign Union2<int, String>
to Union2<String, int> (or the opposite).
On the other hand, it is possible to assign Union2<int, String> to
Union3<int, String, Whatever> (but not the opposite).
Instead we have to wrap our value into another object.
To do so, union expose a bunch of extension methods to transform our
value in a union, which goes from asFirst to asNinth.
This number represent the index that our value correspond to in the type
list that the union can take.
Any union can be freely upcasted to unions that takes more types.
For example, we can do:
Union2<String, int> union2;
Union3<String, int, double> union3 = union2; // upcast, no problem
Union4<String, int, double, Object> union4 = union2; // still works
...
But we cannot downcast unions.
It is therefore impossible to do:
Union3<String, int, double> union3;
// downcast, does not work because the value can be a double too.
Union2<String, int> union2 = union3;
Reading the value
Unions expose their current value publicly through the getter value.
But since the value can be of different types, a small trick is necessary
to preserve type safety:
On a Union2<SomeType, AnotherType>, the type of value is neither of
type SomeType nor of type AnotherType.
Instead it has the type of their nearest common interface.
Union2<String, int> example;
// String and int have no shared, so value is of type Object
Object value = example.value;
int impossible = example.value; // COMPILE ERROR, not type safe
Union2<int, double> example2;
// int and double have a common interface: num. Therefore is of type num
num value2 = example2.value; // type safe, there's no cast or compile error.
int stillImpossible = example2.value; // COMPILE ERROR, value is a num not a int
Union2<String, String> example3;
// The type is always a String, so value is a String too
String value3 = example3.value; // type safe, there's no cast or compile error.
Applying operations on the value
To prevent mistakes, it is not recommended to use the is operator to
determine what the value currently is.
DON'T:
Union2<String, int> union;
if (union.value is String) {
print('String: ${union.value}');
} else if (union.value is int) {
print('int: ${union.value}');
}
This is a bad practice because it is easy to forget to handle one of the
potential types that the value can take.
Instead, use one of the availble methods on unions, such as:
map, which transforms the current value in another value.
switchCase, which allows performing some logic based on the value type.
join, which fuse all the different types in a single type.
This is of course not ideal, but there's an easy way to implement it:
go to the definition of the union you want to reuse (here Union2)
copy its implementation
paste it and update it to match your needs.
On the hand, while the initial boilerplate is a bit boring, this
implementation has some benefits.
Our custom Result<T> is assignable freely to Union2<T, Exception>:
Union2<int, Exception> union2;
Result<int> result = union2; // works without issue
union2 = result; // works too
TopGit's last sync did not record any GitHub topics for rrousselGit/union. GitHub topics appear in the right sidebar of a repository page; that's the authoritative place to check.
How active is development on rrousselGit/union?
The most recent commit recorded on rrousselGit/union was 6.0 years ago, based on the GitHub push timestamp. The repository has 3 forks — one of the better signals of community interest.
How many stars does rrousselGit/union have?
rrousselGit/union has 81 GitHub stars — refresh the page for the live number, or check github.com/rrousselGit/union. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
What language is rrousselGit/union written in?
rrousselGit/union is written primarily in Dart. GitHub's language field is based on the largest share of bytes in the default branch.
Where do I read more about rrousselGit/union?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/rrousselGit/union is the definitive source.
Read full README in the tab above.
Is union worth your time?
ChatGPT, Claude and Perplexity can all read this page. Ask one of them what it makes of union.