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

clone instance(object) #6459

Closed
DartBot opened this issue Nov 1, 2012 · 8 comments
Closed

clone instance(object) #6459

DartBot opened this issue Nov 1, 2012 · 8 comments
Labels
area-library type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Nov 1, 2012

This issue was originally filed by alfniels...@gmail.com


What steps will reproduce the problem?
1.
2.
3.

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?

Please provide any additional information below.

@DartBot
Copy link
Author

DartBot commented Nov 1, 2012

This comment was originally written by alfnielsen...@gmail.com


??
The issue is that there are no clone method!
I can think of the first 1000 ways where I need a clone function/method for an instance.
This is hard to write when the instance is a sub-sub-sub-sub class!
In javascript you can iterate over object properties and make a clone that way.
I know that its because Javascript have a prototype inherit,
but the language should have this funktion if it will be able to do everything javascript does!

@iposva-google
Copy link
Contributor

Added Area-Library, Triaged labels.

@lrhn
Copy link
Member

lrhn commented Nov 3, 2012

We generally don't want ways to create an object that are not going through the class' generative constructors.
You can read what Joshua Bloch says about clone in Java: http://www.artima.com/intv/bloch13.html
Even with that experience in mind, we might be able to avoid the interface problems, but we can't generally decide whether a deep or a shallow copy is the right thing to do. Only the object itself knows that.

I really, really recommend making copy constructors (with named constructors, you can even call it Foo.clone(Foo original)) if you often need to copy an object. It should be no harder than extracting the values you need and redirecting to another generative constructor, e.g.:
   Foo.clone(Foo original) : this(original.id, original.something, new List.from(original.elements));


Removed Type-Defect label.
Added Type-Enhancement label.

@DartBot
Copy link
Author

DartBot commented Nov 4, 2012

This comment was originally written by alfni...@gmail.com


Im sorry but the only thing I can read in Joshuas article is that java have a poor implementation of Cloneable, which I already knew! In dont see why Dart cant implement clone, where it make at true deep copy of the instance without calling the constructor. If Dart wants to be a dynamic language and be able to compete with javascript it should not simply be a copy of java, which is not a dynamic language!

Ist it the point that it wont to be a better version of javascript?

I have the same problem when I write java. Especially when I make game, witch I hoped to do in Dart as well.
In these games I have a monster class thats af subclass of "a moving circle" wicht a sub class of "a circle" witch af subclass of "a shape" witch a subclass of "a point". In some versions the list become much longer!
Yes I can write constructor.clone for each level, but thats not the point.
I javascript I have had the same problems that Joshua talks about, with instance witch point at the same values. But thats just because of a bad made clone function! A clone should of course never share values with the original, that would spoil the point of being a OO language because the object stopes being an instance(unik).
But thats not a problem when you make true deep clones! Its about the way you think of OOP. There we have a "something", and now we need one more of that "something". But I dont want a new instance, I wont a exact copy! (a shallow copy is just a new instance not a clone!)
This is where Javascript is very good, I dont need to write the same things a bunch of times. But as you know Javascript fails because its to hard to keep track of what is actually happening, and you easily write function that you are not totally sure what does.

I can't see Darts become javascript successor, if its not more dynamic than java.
And it think that a shame because you guys have done a very good job in creating Dart,
I especially like named arguments, witch I hope will be implemented in every other language.

@lrhn
Copy link
Member

lrhn commented Nov 4, 2012

Just doing a deep clone sounds easy, but it's almost never correct.
You should never clone "true", "false" or "null", so if your object contains any of these, it's not a real deep clone.
Should you clone a compile-time constant? It's likely that your program will stop working then if it uses the default equality operator. How do you even know if an object is a compile-time constant?

From an OO perspective, Dart (like other similar languages) doesn't distinguish a relation to a separate object and a part-of relation (composition/aggregation). You will want to clone the parts of the object, but not objects it just holds a reference to. Cloning an employee shouldn't create a new company. Only if you know the meaning of each reference in an object are you able to clone it correctly. That's why a simple shallow field clone, or full deep clone, are extremely rarely what you want.

@DartBot
Copy link
Author

DartBot commented Nov 20, 2012

This comment was originally written by dford...@smart-soft.com


Since Dart will not support shared state concurrency I am assuming that means lots of copies will be made. How will this copying be implemented?

@DartBot
Copy link
Author

DartBot commented Feb 17, 2014

This comment was originally written by @chalin


@LRN, thanks for the reference to an online source of Bloch stating that "clone is deeply broken". I just happened to come across this issue. Last week I actually posted a question about Dart and "clone" on SO: http://stackoverflow.com/questions/21744480/clone-a-list-map-or-set-in-dart/21744481#­21744481

I'm curious why this issue is still open. Is it because some consideration is being given to implementing a form of clone method?!?

@lrhn
Copy link
Member

lrhn commented Jul 8, 2015

Still no plan to add a general clone function.

@lrhn lrhn closed this as completed Jul 8, 2015
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed triaged labels Mar 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-library type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants