runtime_type 1.1.0 copy "runtime_type: ^1.1.0" to clipboard
runtime_type: ^1.1.0 copied to clipboard

A non-opaque representation of types at runtime.

example/example.dart

import 'package:runtime_type/runtime_type.dart';

// This is just an example. If you actually want to implement a class like `Value`, just use
// generics :)

class Value<T> {
  Object? _value;

  T get value => _value.castAs(type);

  set value(Object? newValue) {
    if (!newValue.isOfType(type)) {
      throw Exception("$newValue can't be assigned to $type!");
    }

    _value = newValue;
  }

  final RuntimeType<T> type;

  Value(this.type, this._value);
}

void main() {
  final stringValue = Value(stringType, 'foo');
  final intValue = Value(intType, 42);

  final numValue = Value(numType, 17.0);

  /// `numType` accepts the `int` returned by `intValue`
  numValue.value = intValue.value;

  // This, however, will throw
  stringValue.value = 17;

  // While we can initialise a value to an invalid one, accessing it will cause `stringType` to
  // throw an error
  final invalidValue = Value(stringType, 15);
  print(invalidValue.value); // Throws
}
6
likes
140
pub points
64%
popularity

Publisher

unverified uploader

A non-opaque representation of types at runtime.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on runtime_type