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

No convenient way to introduce new functionality to existing types. #40

Closed
lrhn opened this issue Oct 11, 2018 · 6 comments
Closed

No convenient way to introduce new functionality to existing types. #40

lrhn opened this issue Oct 11, 2018 · 6 comments
Assignees
Labels
request Requests to resolve a particular developer problem

Comments

@lrhn
Copy link
Member

lrhn commented Oct 11, 2018

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 than foo.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.

@lrhn lrhn added the request Requests to resolve a particular developer problem label Oct 11, 2018
@lrhn lrhn self-assigned this Oct 11, 2018
@Hixie
Copy link

Hixie commented Oct 11, 2018

Of the proposals, #42 seems the most interesting to me. It feels the most Dart-like.

@willlarche
Copy link

I prefer #41, it's the most intuitive and least intimidating.

@yjbanov
Copy link

yjbanov commented Oct 17, 2018

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.

@yjbanov
Copy link

yjbanov commented Oct 17, 2018

BTW, the first use-case from Flutter that comes to mind, which I think is also better supported by #41 is the .of pattern. I'd prefer to see context.theme and context.localizations, rather than Theme.of(context) and Localizations.of(context).

@munificent has been looking into some stats from existing Flutter code and found that .of is quite popular.

@jmesserly
Copy link

Excited to see progress on this! This would be incredibly helpful for JS interop too (dart-lang/sdk#35084).

@mit-mit
Copy link
Member

mit-mit commented Jan 7, 2020

Closing as we shipped support for static extension methods, #41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

6 participants