Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Kotlin like extension functions #32280

Closed
MikeMitterer opened this issue Feb 22, 2018 · 19 comments
Closed

feature: Kotlin like extension functions #32280

MikeMitterer opened this issue Feb 22, 2018 · 19 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug

Comments

@MikeMitterer
Copy link

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

@lrhn
Copy link
Member

lrhn commented Feb 22, 2018

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 (#9991).

@lrhn lrhn added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug labels Feb 22, 2018
@svenjacobs
Copy link

@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 :)

@passsy
Copy link

passsy commented Mar 10, 2018

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");
  }
}

@MikeMitterer
Copy link
Author

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"!

@loic-sharma
Copy link

loic-sharma commented Mar 20, 2018

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.

@TarasMazepa
Copy link

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.

@marcguilera
Copy link

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?

@mpiannucci
Copy link

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

@lrhn
Copy link
Member

lrhn commented Jun 22, 2018

Closing this as a duplicate of #9991.

@lrhn lrhn closed this as completed Jun 22, 2018
@lrhn lrhn added the closed-duplicate Closed in favor of an existing report label Jun 22, 2018
@erluxman
Copy link

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.

@dgrove
Copy link
Contributor

dgrove commented Dec 11, 2018 via email

@BaraaSoft
Copy link

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

@eernstg
Copy link
Member

eernstg commented Apr 3, 2019

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

@derolf
Copy link

derolf commented Apr 11, 2019

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!

@pedromassango
Copy link

Please Dart team add it. We need this

@NanoMichael
Copy link

That be very attractive to add this feature.

@RyanSusana
Copy link

Let's adddddd this!

@levirgon
Copy link

Add this, please. 👍

@creativecreatorormaybenot
Copy link
Contributor

@levirgon It is available starting with Dart 2.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests