Skip to content

Add default methods to allow evolutions on classes #30416

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

Closed
a14n opened this issue Aug 11, 2017 · 5 comments
Closed

Add default methods to allow evolutions on classes #30416

a14n opened this issue Aug 11, 2017 · 5 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 core-m customer-google3 type-enhancement A request for a change that isn't a bug

Comments

@a14n
Copy link
Contributor

a14n commented Aug 11, 2017

I really like Dart implicit interface (every classes can be used as interface) but it makes harder to make evolutions on classes.
For instance it's almost impossible today to add a method to Map or List without releasing a major version.
In Java, interfaces can evolve with the addition of default methods. Those methods are implemented only based on the interface methods.

I wondered if something like that could be done in Dart.

For instance, we could add a method groupBy on Map :

class Map<K,V> {
  // new method that will be used by classes implementing Map without defining groupBy
  default Map<K,List<E>> groupBy<K>(K key(E element)) {
    // my implementation that does not used private members
  }
}

class MyMap<K,V> implements Map<K,V> {
}

and my class MyMap that implements Map would have the new method without breakage. It would break only if the implementation as a method with the same name but with different parameters.

WDYT?

@anders-sandholm anders-sandholm added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Aug 11, 2017
@matanlurey
Copy link
Contributor

matanlurey commented Aug 13, 2017

+1. We strongly need either this and/or something like C#'s extension methods.

Otherwise it is too painful to evolve any library, which leads to a lot of fragmentation.

@lrhn lrhn added core-m type-enhancement A request for a change that isn't a bug labels Aug 14, 2017
@floitschG floitschG removed the core-m label Aug 14, 2017
@eernstg
Copy link
Member

eernstg commented Aug 15, 2017

In the language team we have discussed adding support for default methods for a while, using exactly the same motivation as given here (to allow widely used interfaces to grow). I've added a reference to this issue in an internal language design issue, to ensure that we're aware of this input on the matter.

@matanlurey
Copy link
Contributor

The lack of something like this just made a ~5 minute task into a ~5 hour one :(

@a14n
Copy link
Contributor Author

a14n commented Mar 16, 2020

See dart-lang/language#884

@lrhn lrhn added the closed-duplicate Closed in favor of an existing report label May 11, 2023
@lrhn
Copy link
Member

lrhn commented May 11, 2023

Yep, deferring this to the language repo issue.

@lrhn lrhn closed this as completed May 11, 2023
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 core-m customer-google3 type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants