Tensegritics / ClojureDart

Clojure dialect for Flutter and Dart
1.43k stars 92 forks source link

Reify all literal types #11

Open cgrand opened 2 years ago

cgrand commented 2 years ago

All classes appearing as literals should be reified so has to allow for a true instance? function and multimethods hierarchies (#3).

Methods exposed by a reify type should allow to test if an instance is of this type and if a type is a subtype of another.

Subtyping matrix can be determined statically and only needs to cover types appearing as literals.

cgrand commented 1 year ago

These reified types could also brings way to call constructors, and even in a "data-driven" way (spreading vector/map).

cgrand commented 1 year ago

Types are rarely used in Dart APIs so we could consider changing the evaluation of a class name from a Type instance to something ClojureDart specific. This would have the benefit that instance? could work (adieu dart/is?). At first I thought that this value should be a function too so as to be used to create instances -- even taking maps as arguments! However with named constructors it's not so clear and I'm not sure bundling all constructors together is a good idea. Maybe the more generic idea of doing "clojurization" for tear-offs. Syntactically we could leverage var: #'m/ListView.separated would evaluate to a Clojure fn taking named parameters as maps.

cgrand commented 1 year ago

into-array accepts a type parameter that we can't observe, it's just there for compatibility with Clojure. with reified literal classes we could imagine making it work but it would require adding a "makeFxiedList" method to class literals. Then it begs the question: why to stop at Lists, what if I want sets or any other kind of typed collections? Map is a good example because now you need two types, so it means it can't be just a function of a single type literal. Special-casing for lists and only lists may be ok after all...

cgrand commented 1 year ago

Another use-case: object to map conversion (bean-like) https://clojurians.slack.com/archives/C03A6GE8D32/p1697712372844039

cgrand commented 1 year ago

class Class<T> {
  final Set<Type> ancestors;
  bool isInstance(dynamic x) => x is T;
  bool descendsFrom(Class klass) => ancestors.contains(klass.type);
  Type get type => T;
  const Class([this.ancestors = const {}]);
  @override
  operator == (dynamic other) => (other is Class) && (other.type == type);
  @override
  get hashCode => T.hashCode ^ 19870801; 
}

main()  {
  final x = <dynamic>[];
  final y = <String>[];
  print(x.runtimeType);
  print(y.runtimeType);
  print(x.runtimeType == y.runtimeType);
  const list = Class<List>();
  const strings = Class<List<String>>();
  print(list.isInstance(2));
  print(list.isInstance(x));
  print(list.isInstance(y));
  print(strings.isInstance(x));
  print(strings.isInstance(y));
  const string = Class<String>({Object});
  const object = Class<Object>();
  print(string.descendsFrom(object));
}
cgrand commented 11 months ago

A Flutter class using Type objects https://api.flutter.dev/flutter/widgets/Actions/Actions.html