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

Copying simple objects #35309

Open
qis opened this Issue Dec 3, 2018 · 4 comments

Comments

6 participants
@qis

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

This comment has been minimized.

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.)

@zoechi

This comment has been minimized.

Contributor

zoechi commented Dec 4, 2018

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

@qis

This comment has been minimized.

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).

@fabiocarneiro

This comment has been minimized.

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;
      }
}

@lrhn lrhn added this to Non-Breaking and Complex in Language Enhancement Categories Dec 14, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment