1

I am trying to understand self language.

My doubt is that, whether cloning in self language deep clone or shallow one. I.e., whether clone just clones objects slots or objects inside the slots are get cloned.

Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72
shakthi
  • 1,597
  • 2
  • 17
  • 29
  • Do you mean shallow copies? – Tobias Jul 21 '13 at 11:55
  • Yes. Let me clear my self. Consider a self object bankAccount. Say it consists of object accountholder = Person. Consider I have cloned bank Account to create anewBankaccount. Now if I changed anewBankaccount.accountholder.name. Does it affect to the original object bankAcclount from which have I cloned? – shakthi Jul 21 '13 at 12:06

2 Answers2

3

As Tobias says, typically the copy message is implemented as a shallow copy.

Semantically, copy in Self pretty much means 'give me a safe/useful copy'. For many objects this is just a shallow copy.

Objects are responsible for implementing their own copy, either by delegating to a pure shallow copy method such as the implementation in traits clonable or by having their own copy slot.

For example, morphs typically respond to the copy message by giving you a usable copy, rather than a shallow copy. Also objects which shouldn't be copied often implement or delegate to a method copy = (self) so that attempts to copy just return the original object.

If you want a strict shallow copy, most copiable objects will respond to clone though this should be used with care. And at the base is the VM defined primitive method _Clone which is a shallow copy.

Russell Allen
  • 253
  • 1
  • 6
1

Typically, clones in self are shallow copies.

Regarding your comment, yes it would change. That is why you clone “empty” prototypes.

Note that self knows about copy-downs to selectively copy slot content one level deeper when you clone a prototype to make a new one.

Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72
Tobias
  • 3,026
  • 20
  • 34
  • Thanks for the quick info. To be honest I could not understand copy-down. As a newbie, all I got is this jargon: `Copy-down is not part of the Self language but a mechanism in the Self programming environment` which solves the problem inheriting data member. – shakthi Jul 21 '13 at 13:00
  • Well, Self does not use classes for code sharing but prototypes; template object you clone to create new, “real” ones. When you want to create new _prototypes_ you have to clone old ones. In this case you want to copy slots over, that is what copy-downs are for. It says “not part of the Self language” refers to the fact, that copy-donws are not language constructs but simple messages. Anyway, in your case, you probably just want a [_slot initializer_](http://docs.selflanguage.org/4.5/langref.html#slot-descriptors) that clones a new `accountholder` when you clone the `bankAccount`. – Tobias Jul 21 '13 at 18:18
  • @thobias I need a small clarification, Consider bankAccount has parent bankAccountTrait. Say bankAccountTrait has a slot named "bankname" . Does setting "bankname" in bankAccount changes value of slot on bankAccountTrait? I guess answer is no. If so can you please explain how a method able to modify its receiver.(I just understand that 'self*' is sent as an parent to a method) – shakthi Jul 27 '13 at 19:02
  • I wanted to make above comment a separate question. But I failed to phrase it properly. – shakthi Jul 27 '13 at 19:10