Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/abitofevrything/runtime_type
A non-opaque representation of types at runtime.
https://github.com/abitofevrything/runtime_type
dart types
Last synced: 25 days ago
JSON representation
A non-opaque representation of types at runtime.
- Host: GitHub
- URL: https://github.com/abitofevrything/runtime_type
- Owner: abitofevrything
- License: mit
- Created: 2022-12-03T15:41:14.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-28T21:32:57.000Z (12 months ago)
- Last Synced: 2024-10-14T09:28:32.672Z (2 months ago)
- Topics: dart, types
- Language: Dart
- Homepage: https://pub.dev/packages/runtime_type
- Size: 18.6 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# runtime_type
A non-opaque representation of types at runtime.
Normally, `Type` objects are opaque and cannot be used for actual logic. `RuntimeType` allows for operations such as sub- or super-type checking, type checks, and casting using a variable instead of a literal.
To use, replace your type literals with calls to the `RuntimeType` constructor:
```dart
// Before
final stringType = String;
// After
final stringType = RuntimeType();
```### A word of warning
Please only use this library if you're sure it's what you want. If you're not completely at ease with the Dart language, ask someone who is whether your problem can be solved some other way, as Dart's type system is normally enough to handle problems.
### How it works
Instead of using `Type` instances to track information about a type, `RuntimeType` uses a generic type to hold the same information. Then, by using that generic and Dart's handling of generic subtype checking, we can implement most of the functions that normal type literals have.
If you've seen this trick before, this package uses the same principle:
```dart
bool isTypeASubtypeOfB() => [] is List;
```However, that trick does not work when `A` and `B` cannot be resolved statically - which is why this package creates `RuntimeType` objects statically, but which can then be passed around and used dynamically as the type information is held within the object itself.