1

What's it called when you tell an object to clone all of its arguments (like in deep cloning), but the top-level object isn't changed?

I need to implement this sort of method in a program, and I'm just trying to figure out what to call the method.

Say that we're cloning Foo, which is referenced by several other objects. If we were doing deep-level cloning, then our clone's arguments would be inaccessible to other objects which reference Foo. In this (currently unidentified) type of cloning, if we clone Foo, then other objects referencing Foo would have access to the the newly cloned arguments.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
Nat
  • 1,085
  • 2
  • 18
  • 35
  • I think what's more relevent is why are you doing this? For a generic object, this makes no sense. Obviously it has sense to you, so you are working with details that are relevent. – Mooing Duck Jun 27 '14 at 21:59
  • 1
    You say "clone all of its arguments". It's not clear what you mean by that (objects don't have arguments), but the best guess I have would be [defensive copying](http://www.javapractices.com/topic/TopicAction.do?Id=15). – user2357112 Jun 27 '14 at 22:02
  • 1
    It sounds like you're doing: `forall attr in obj: obj.attr := deepcopy(obj.attr)` Is that correct? – thebjorn Jun 27 '14 at 22:03
  • @ChemicalEngineer: It sounds like you aren't cloning at all. You're passing the top-level object by reference, and then reassigning a new value to `Foo`. – Mooing Duck Jun 27 '14 at 22:03
  • @thebjorn: Yes, exactly. – Nat Jun 27 '14 at 22:06
  • @ChemicalEngineer: Does `foo` share members with other instances of `foo`? And you want to clone the members so they're no longer shared? It sounds vaguely like copy-on-write – Mooing Duck Jun 27 '14 at 22:07
  • @Mooing Duck: I'm working with an abstract syntax tree in which all nodes are immutable. In some cases I want to deep copy all of the arguments, but not the top-level item itself, before performing simplification checks. It's a simple method to write, as thebjorn showed, though I'm not sure what to call the method such that others would recognize it. Seems like it'd be common enough to have a name. – Nat Jun 27 '14 at 22:09
  • 1
    I don't think there's a name for that. I'd suggest naming the method with something that relates to the reason why you're doing this. – thebjorn Jun 27 '14 at 22:10
  • @thebjorn: Honestly I'm stretching the definitions of some of these things. For example, I'm calling it "deep simplification" when the top-level expression is simplified with all parameters also simplified (e.g. `1^1+1+2`->`4`), "shallow simplification" when only the top-level expression is simplified (e.g. `1^1+1+2`->`1^1+3`), and, well, not sure when only parameters are simplified (e.g. `1^1+1+2`->`1+1+2`). Made this question about cloning since it's a simple case of tree-type logic. Anyway, if there's not a name, I'll accept that as an answer if you'd submit it. – Nat Jun 27 '14 at 22:17
  • 1
    Your current namings seem very sensible (at least it will make it easy to figure the code out six months from now). parameter_simplification seems like the logical choice..? – thebjorn Jun 27 '14 at 22:25
  • @thebjorn: Yup, that sounds like the way to go! – Nat Jun 27 '14 at 22:31

1 Answers1

2

I don't think there's a name for that. I'd suggest naming the method with something that relates to the reason why you're doing this.

thebjorn
  • 26,297
  • 11
  • 96
  • 138