Can I pass an extension method as a parameter?

66 views
Skip to first unread message

Dale King

unread,
Dec 5, 2019, 1:17:54 PM12/5/19
to Dart Misc
So been playing with the new extension methods and I have to ask isn't there any way to pass an extension method method as a parameter? I have read the section on tearoffs, but the example there is stupid as it declares an extension on a type, but never actually references that type
At the very list isn't there a way to take an extension like:

extension Foo on int { int doubled() => this * 2 }

and turn Foo.doubled into an instance of int Function(int)?

Nate Bosch

unread,
Dec 5, 2019, 1:42:02 PM12/5/19
to General Dart Discussion
Extension methods are syntactically closer to an instance method than a static function. You can tear off an extension method where it is applied on an instance, not statically. That is `1.doubled` will tear off an `int Function()`. To get an `int Function(int)` you'll need to wrap it as `(int i) = i.doubled()`.

--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/3f92ef96-1744-4268-a2cd-2dac7134bed7%40dartlang.org.

Dale King

unread,
Dec 5, 2019, 3:37:48 PM12/5/19
to Dart Misc
Yeah, I think that this is not specific to extension methods and rather a missing feature of Dart with regard to instance methods.

For comparison here is the documentation for method references in Java which is the equivalent of Dart tear-offs: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html. Java supports 4 types of method references:

Reference to a static methodContainingClass::staticMethodName
Reference to an instance method of a particular objectcontainingObject::instanceMethodName
Reference to an instance method of an arbitrary object of a particular typeContainingType::methodName
Reference to a constructorClassName::new

So Dart only supports the first 2 cases, a reference to a top-level or static function or a reference to an instance method of a particular object. It does not support references to an instance method of an arbitrary object.

For dart it would look like Foo.doubled would an expression of type `int Function(int)` that would be the equivalent of a lambda of the form `(int x) => x.doubled()`.

So basically Dart does not treat instance methods as first class citizens and they cannot be passed to higher level functions.

Really wish we had Flutter built on a real language like Kotlin.

On Thursday, December 5, 2019 at 1:42:02 PM UTC-5, Nate Bosch wrote:
Extension methods are syntactically closer to an instance method than a static function. You can tear off an extension method where it is applied on an instance, not statically. That is `1.doubled` will tear off an `int Function()`. To get an `int Function(int)` you'll need to wrap it as `(int i) = i.doubled()`.

On Thu, Dec 5, 2019 at 10:17 AM Dale King <dale...@gmail.com> wrote:
So been playing with the new extension methods and I have to ask isn't there any way to pass an extension method method as a parameter? I have read the section on tearoffs, but the example there is stupid as it declares an extension on a type, but never actually references that type
At the very list isn't there a way to take an extension like:

extension Foo on int { int doubled() => this * 2 }

and turn Foo.doubled into an instance of int Function(int)?

--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mi...@dartlang.org.

Erik Ernst

unread,
Dec 6, 2019, 10:11:48 AM12/6/19
to Dart Misc, Dale King
There's a request for constructor tear-offs in https://github.com/dart-lang/language/issues/216, and that request has substantial support and might well be added to Dart. Similarly, https://github.com/dart-lang/language/issues/691 proposes some generalizations covering setters and getters, and the case where `Class.member` is used to tear off a function that accepts the receiver as an extra argument would fit nicely into that approach.

So we may not cover all the cases yet, but these ideas seem useful! ;-)

Dale King

unread,
Dec 6, 2019, 12:00:00 PM12/6/19
to Dart Misc, dale...@gmail.com
Good to know. Those other cases are vital. In the end I refactored the code in a different way where I did not need to pass them, but it would be nice to have that ability.
Reply all
Reply to author
Forward
0 new messages