-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
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 There are multiple other options, all with different trade-offs, because nothing is ever easy :) |
@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 Example 1Wrapping 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 2Problem: 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 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 |
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 |
dart-lang/language#41 is where this is being
worked on.
…On Mon, Dec 10, 2018 at 6:02 PM Laxman Bhattarai ***@***.***> 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
<#32280 (comment)>,
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. 👍 |
@levirgon It is available starting with Dart |
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
The text was updated successfully, but these errors were encountered: