Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Add castCallback1->N to package:angular/meta.dart #1489

Closed
matanlurey opened this issue Jul 12, 2018 · 2 comments
Closed

Add castCallback1->N to package:angular/meta.dart #1489

matanlurey opened this issue Jul 12, 2018 · 2 comments

Comments

@matanlurey
Copy link
Contributor

matanlurey commented Jul 12, 2018

In AngularDart v5 with Dart2, the lack of reified generics definitely hurts:

typedef ItemRenderer<T> = String Function(T);

abstract class Dog {
  String get name;
}

class MyComponent {
  String itemRenderer(Dog dog) => dog.name;
}

class ItemListComponent<T> {
  ItemRenderer<T> itemRenderer;
}

void main() {
    var myComponent = new MyComponent();
    var childComponent = new ItemListComponent<dynamic>();

    //  TypeError: Type '(Dog) => String' is not a subtype of type '(dynamic) => String'
    childComponent.itemRenderer = myComponent.itemRenderer;
}

We're going to work on #68, but until then, users need an ergonomic way to untype these types of functions/closures, but retain the some benefits of static typing (IDE auto-completion, go-to definition, avoid dynamic calls).

My proposal is to add a package:angular/meta.dart (we were already planning this for @visibleForTemplate), and include a few helper functions to cast callbacks into forms that are suitable for AngularDart components, with the goal of removing/fixing these cases when we fix #68:

T Function(dynamic) castCallback1<T, A>(T Function(A) callback) {
  return (dynamic element) => callback(element as A);  
}

You can see a full proposal, and example, on DartPad:
https://dartpad.dartlang.org/82e79cbcd0c781b9cda557754fbccbf8

(I am proposing this for v5 final, as a migration aid from v4)

Another advantage is we can leverage unsafeCast (internal function) to reduce the amount of runtime casts that occur in production code, retaining performance from v4 (and Dart 1) as much as possible.

@alorenzen
Copy link
Contributor

Not sure why it needs to live in angular, put understand the reasoning for having it somewhere.

Maybe we could just stick it in quiver?

@matanlurey
Copy link
Contributor Author

matanlurey commented Jul 12, 2018

We don't have a dependency on quiver (intentionally, it breaks too often). The reason for Angular is to track the usages of it, and once we have proper generic support we could @deprecate it with references on how to use template generics. We could call this castCallbackForAngular if we want to make that more clear, of course.

@matanlurey matanlurey self-assigned this Jul 13, 2018
matanlurey added a commit that referenced this issue Jul 13, 2018
Closes #1489.

PiperOrigin-RevId: 204490128
chalin added a commit that referenced this issue Jul 17, 2018
- Fix typo in issue number: `#1849` -> #1489
- Add a reference to: Template syntax should support tear-offs for event bindings #506

cc @kwalrath
chalin added a commit that referenced this issue Jul 17, 2018
- Fix typo in issue number: `#1849` -> #1489
- Add a reference to: Template syntax should support tear-offs for event bindings #506

cc @kwalrath
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants