Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reify all literal types #11

Open
cgrand opened this issue Apr 13, 2022 · 6 comments
Open

Reify all literal types #11

cgrand opened this issue Apr 13, 2022 · 6 comments

Comments

@cgrand
Copy link
Contributor

cgrand commented Apr 13, 2022

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
Copy link
Contributor Author

cgrand commented Apr 18, 2023

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

@cgrand
Copy link
Contributor Author

cgrand commented Aug 17, 2023

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
Copy link
Contributor Author

cgrand commented Oct 3, 2023

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
Copy link
Contributor Author

cgrand commented Oct 19, 2023

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

@cgrand
Copy link
Contributor Author

cgrand commented Nov 3, 2023


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
Copy link
Contributor Author

cgrand commented Dec 4, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant