Skip to content

Copying simple objects #35309

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
qis opened this issue Dec 3, 2018 · 4 comments
Closed

Copying simple objects #35309

qis opened this issue Dec 3, 2018 · 4 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-stale Closed as the issue or PR is assumed stale core-n type-enhancement A request for a change that isn't a bug

Comments

@qis
Copy link

qis commented Dec 3, 2018

Is the dart-lang team willing to re-visit the idea of a dedicated copy function that allows users to create a deep or shallow copy of objects if it can be proven that there are no side effects (no setters present, etc.)?

How about a dedicated copy constructor/operator similar to C++ for when such a copy cannot be done safely?

The last issue I could find was created/updated over 6/3 years ago: #3367

Dart 2.1.0 (build 2.1.0-dev.9.4 f9ebf21)

@eernstg
Copy link
Member

eernstg commented Dec 3, 2018

I think this would be a natural part of the design of a 'value class' feature: We'd want to be able to create a copy of an existing value, except that the copy method would have named arguments corresponding to the fields, such that we could perform a "functional update" on the value, that is, we could get a copy which differs as specified. So apart from the fact that it might end up having the name update, it would likely amount to passing the empty set of parameters.

Would this be sufficient for your purposes, or would it be essential/helpful/convenient for that copy operation to work on mutable objects as well?

(I'm sure we have an issue where 'value classes' or 'value types' are requested, but I can't find it right now; but the idea is that you get a concise notation for declaring a class where all fields are final; equality is implemented by code generation in the compiler as a deep comparison, recursively following all references that are themselves value types; and hashCode is generated correspondingly.)

@vsmenon vsmenon added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Dec 3, 2018
@zoechi
Copy link
Contributor

zoechi commented Dec 4, 2018

https://pub.dartlang.org/packages/built_value provides this currently via code generation

@qis
Copy link
Author

qis commented Dec 5, 2018

@eernstg As long as the functionality exists, I personally do not care about the name. On that note, the naming convention in flutter for this would be copyWith (for example in the TextStyle class).

@lrhn lrhn added type-enhancement A request for a change that isn't a bug core-n labels Dec 6, 2018
@fabiocarneiro
Copy link

fabiocarneiro commented Dec 11, 2018

I would appreciate if this was natively supported by the language. It makes it very convenient for value types.

Its particularly useful on a Mixin (or trait), when you are providing code but you don't know the final type or will be applied to multiple types. The concrete type can't be referenced but one would be able to clone this and change the values on the cloned object.

mixin AType on BType {
     int usefulProperty;
     
     BType increaseUsefulness() {
           BType newObject = clone this;
           newObject.usefulProperty = this.usefulProperty + 1;

           return newObject;
      }
}

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-stale Closed as the issue or PR is assumed stale core-n type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants