Sdk: feature: Kotlin like extension functions

Created on 22 Feb 2018  Â·  19Comments  Â·  Source: dart-lang/sdk

I know, I know - not possible for Dart 2 but maybe for Dart 2.1???

Extension functions are really!!!! cool in Kotlin - would be cool for Dart as well
https://kotlinlang.org/docs/reference/extensions.html

area-language closed-duplicate type-enhancement

Most helpful comment

@lrhn Yes, Kotlin's extension functions are "just" syntactic sugar and there are several other options in Dart (and other languages in general) like you described. However I think the point is that they are syntactic sugar and that they can be called like functions on the original class. This enables some nice, concise syntax. I, too, would like to see the support for extension functions (and properties?) in a future Dart version :)

All 19 comments

The Kotlin extension functions are a version of scoped and statically-resolved extension methods, similar to the ones in C#. That makes them easy to implement, but they're also just a type-based shorthand for calling a static method. It allows someone else to extend a type, but it doesn't allow a type to specialize the implementation. It also allows conflicts between different extensions and between an extension and the type itself.

Another option is to define the extension methods on new "static types" that adapt the original types, so instead of adding List<T>.rotate(int n), you introduce a new type MyList<T> that "extends" List<T> and has a rotate(int n) method. Then you do MyList<T> myList = list; myList.rotate(42);.
That will allow you to choose to adapt any list, and avoid conflicts between different extensions.
(It's list traits, basically).

There are multiple other options, all with different trade-offs, because nothing is ever easy :)
It's not a new request (https://github.com/dart-lang/sdk/issues/9991).

@lrhn Yes, Kotlin's extension functions are "just" syntactic sugar and there are several other options in Dart (and other languages in general) like you described. However I think the point is that they are syntactic sugar and that they can be called like functions on the original class. This enables some nice, concise syntax. I, too, would like to see the support for extension functions (and properties?) in a future Dart version :)

I really want extension functions in dart. Especially the Iterable API is lacking some features and it would be super handy to be able to add them myself and even publish them as package.

Example 1

Wrapping is not bad but sometimes breaks patterns.

With extension function

// with proposed extension functions
// distinctBy not in sdk, but works nicely with extension function
list.where((it) => it.size > 4)
    .map((it) => it.id)
    .distinctBy((it) => it)
    .skipWhile((it) => isValidId(it))

List<T> List<T>.distinctBy<T>(bool selector(T t)) => /*...*/;

Current solution

// current state of the art
// without extension and wrapping, therefore unreadable
distinctBy(list.where((it) => it.size > 4)
    .map((it) => it.id), (it) => it)
    .skipWhile((it) => isValidId(it));

List<T> distinctBy<T>(List<T> l, bool selector(T t)) => /*...*/;

Example 2

Problem:

void doSomething(List<String> list) {
  // crashing for empty list with "No element"
  // firstOrNull not in sdk
  if (list.first == "abc") {
    print("found");
  }
}

Solution with extension function

// proposed extension function
T List<T>.firstOrNull<T> => this.isEmpty ? null : this.first;

void doSomething(List<String> list) {
  // nearly unchanged code
  if (list.firstOrNull == "abc") {
    print("found");
  }
}

Current solution:

// normal helper function, requires wrapping
T firstOrNull<T>(List<T> l) => l.isEmpty ? null : l.first;

void doSomething(List<String> list) {
  // fixed but list is now wrapped
  if (firstOrNull(list) == "abc") {
    print("found");
  }
}

The more I use extension functions the more I come to the conclusion that they are fare from "just syntactic sugar" - they are one of Kotlins "killer features"!

Extension methods are wonderful for composition. For example, an app can compose extensions to List from two different package dependencies. Composition doesn't work if everyone has extended List with their own custom functionality.

Also, extensions are also great for adding common functionality to ALL children of a certain type. I can add my custom app-specific filter to List and Set with one extension method.

I would love to see Kotlin-like extension functions in Dart :) It really saves lots of time. You do not need to search multiple util or helper classes or helper functions which were not connected to any class. But with extension functions all that you need is to type "variable." and you will see all methods and extensions classes available for this instance.

It might "just" be sugar but it would be awesome. I find myself extending some classes like Observable just to add some functionality when I would like to declare the extension and be able to use it with the original instance elsewhere in the codebase. Is this on the radar at all?

Just want to chime in that this would be incredibly useful for Flutter development.

Closing this as a duplicate of #9991.

If Fast Development is Flutter's USP and Dart cares for Flutter, then I think community deserves to get what they are asking for Extension Function in Dart.

https://github.com/dart-lang/language/issues/41 is where this is being
worked on.

On Mon, Dec 10, 2018 at 6:02 PM Laxman Bhattarai notifications@github.com
wrote:

If Fast Development is Flutter's USP and Dart cares for Flutter, then I
think community deserves to get what they are asking for Extension
Function in Dart.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/dart-lang/sdk/issues/32280#issuecomment-446044735,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACAsW9fnnIhB7ly6f0e1eqXJbbF8k_8Hks5u3xIYgaJpZM4SPRWS
.

I agree the extension method is wonderful, it must be added to Dart.

Cf. dart-lang/language#41, dart-lang/language#42, dart-lang/language#177 for current discussions on this topic.

I am working on a project where the backend is written in Kotlin and the frontend in Dart (Flutter), so I am switching back and forth the whole day.

Extension functions and null-pointer-safety are definitely the number 1 features I miss!

Please Dart team add it. We need this

That be very attractive to add this feature.

Let's adddddd this!

Add this, please. :+1:

@levirgon It is available starting with Dart 2.6.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

inohunk picture inohunk  Â·  47Comments

rxwen picture rxwen  Â·  80Comments

besthyhy picture besthyhy  Â·  55Comments

Hixie picture Hixie  Â·  46Comments

rmacnak-google picture rmacnak-google  Â·  108Comments