-
Notifications
You must be signed in to change notification settings - Fork 205
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
No convenient way to introduce new functionality to existing types. #40
Comments
Of the proposals, #42 seems the most interesting to me. It feels the most Dart-like. |
I prefer #41, it's the most intuitive and least intimidating. |
Specifically for extending functionality of existing types I think #41 is the better solution among the proposals so far, despite having a higher risk of name collisions. #42 could be useful too if it had the semantics of Golang's defined types. |
BTW, the first use-case from Flutter that comes to mind, which I think is also better supported by #41 is the @munificent has been looking into some stats from existing Flutter code and found that |
Excited to see progress on this! This would be incredibly helpful for JS interop too (dart-lang/sdk#35084). |
Closing as we shipped support for static extension methods, #41 |
Current status: being addressed via #41
The author of a Dart class can add more features to the class, but down-stream clients cannot easily do so.
To work with an existing class, you can write a static helper function, but it's much less convenient to call that as a function than as a method on the object. Example:
helper(foo.something).somethingElse
is harder to read and understand the sequencing of thanfoo.something.helper().somethingElse
.The best current option is to wrap the existing class in a new class which forwards members to the wrapped object, and then adds some more operations on top. It adds overhead and requires an explicit wrapping operation.
Other languages solve this problem in various ways, including extension methods and pipe operators.
Dart should do something to make it easier for users to add easily usable functionality to existing classes.
The text was updated successfully, but these errors were encountered: