Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

generic methods #28

Closed
jmesserly opened this issue Jan 21, 2015 · 10 comments
Closed

generic methods #28

jmesserly opened this issue Jan 21, 2015 · 10 comments

Comments

@jmesserly
Copy link
Contributor

methods on Iterable, like map, could really use a generic method annotation. Even if we had to do inference for those methods, it would be worth it IMO. Annotation could be opt in.

Some ideas:

// current signature/impl
Iterable map(f(E element)) => new MappedListIterable(this, f);

// full signature in an annotation
@Sig('(E -> R) -> Iterable<R>')
Iterable map(f(E element)) => ...

// Tells the system to use inference like H-M for this method
// This could be added by the programmer in places where it is needed.
// User friendly but presumably more compute intensive.
// It could also be a convenience feature on top of the explicit signature.
@inferTypes
Iterable map(f(E element)) => ...
@jmesserly
Copy link
Contributor Author

fyi @pq :)

@pq
Copy link

pq commented Jan 21, 2015

Yes. It should come as no surprise to you that I completely agree! :)

@jmesserly
Copy link
Contributor Author

another idea, it's a bit verbose, but typedefs allow generic methods. so it could be encoded like this:

typedef Iterable<R> IterableMapFn<T, R>(Iterable<T> self, R f(T t));
// ...
@GenericMethod(IterableMapFn)
Iterable map(f(E element)) => ...

verbose, yes... but it's kind of neat that it uses the Dart language? Maybe an okay stopgap, assuming these generic methods aren't all that common?

for a complete example that runs on VM:

import 'dart:mirrors';

typedef List<R> IterableMapFn<T, R>(Iterable<T> self, R f(T t));

class Sig {
  final fn;
  const Sig(this.fn);
}

class Mapper<T> {
  List _data;
  Mapper(this._data);

  @Sig(IterableMapFn)
  List mapToList(f(T t)) {
    var result = [];
    for (var i in _data) result.add(f(i));
    return result;
  }
}

main() {
  print(new Mapper<int>([1,2,3]).mapToList((x) => 'A$x'));
  print(reflectType(Mapper).declarations[#mapToList].metadata.first.reflectee.fn);
}

@jmesserly
Copy link
Contributor Author

@jacob314 made a good point, which you can see in those code examples, they create Iterable/Lists without reified types. Not sure how to address this in the Dart codegen, but seems like it could work in JS codegen?

@jmesserly
Copy link
Contributor Author

I think @leafpetersen is looking into this one

@jmesserly
Copy link
Contributor Author

This is pretty far along now. We have comments in SDK, basic upwards inference. In a few places we need to support explicitly passing type arguments (for calls & tear offs)

@jmesserly
Copy link
Contributor Author

static checking is essentially done, we need the JS compilation side of things

@nex3
Copy link

nex3 commented Jan 22, 2016

Just for reference, what's the syntax for this?

@jmesserly
Copy link
Contributor Author

@jmesserly jmesserly mentioned this issue Jan 29, 2016
12 tasks
@jmesserly
Copy link
Contributor Author

Closing this one out, since we definitely decided to do it, and are well along in the implementation.
New tracking bug here: #435

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

5 participants