TopGit theo dõi rrousselGit/union trên GitHub, đã đạt 81 sao. Union types for dart
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
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
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/rrousselGit/union là nguồn chính thức.
rrousselGit/union có bao nhiêu sao?
rrousselGit/union có 81 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/rrousselGit/union. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
rrousselGit/union có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho rrousselGit/union. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
rrousselGit/union còn đang phát triển không?
Commit gần nhất trên rrousselGit/union là 6.0 năm trước (theo timestamp GitHub). Repo có 3 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
rrousselGit/union viết bằng ngôn ngữ gì?
rrousselGit/union chủ yếu viết bằng Dart. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về union?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về union.