-
Notifications
You must be signed in to change notification settings - Fork 26
generic methods #28
Comments
fyi @pq :) |
Yes. It should come as no surprise to you that I completely agree! :) |
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);
} |
@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? |
I think @leafpetersen is looking into this one |
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) |
static checking is essentially done, we need the JS compilation side of things |
Just for reference, what's the syntax for this? |
Syntax is described here: https://github.com/dart-lang/dev_compiler/blob/master/GENERIC_METHODS.md |
Closing this one out, since we definitely decided to do it, and are well along in the implementation. |
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:
The text was updated successfully, but these errors were encountered: