-
Notifications
You must be signed in to change notification settings - Fork 211
Description
From #711:
Quick question regarding method names, we have
Variant::clone()GodotString::clone()Dictionary::duplicate()VariantArray::duplicate()VariantArray::duplicate_deep()And for ref-increment we use
new_ref(), if I'm not mistaken.From a user perspective, maybe we should highlight this somewhere in the documentation?
Is there a semantic difference between
cloneandduplicate[_deep]or is the idea "when it supports both shallow and deep copy, useduplicate/duplicate_deep, otherwise useclone?
I'm aware that it's a bit messy right now. For
clonevsduplicate, my understanding is thatCloneon a reference-counted type should be a reference increment (i.e. not even a shallow copy), to be consistent with standard types likeRc,Arc, even though the reference-counting is "internal" in the case of variant collections. That does makeNewRefseem redundant, and I think that would warrant a separate issue for discussion.
I agree with the Rc consistency, but keep in mind that in the standard library, the boundary between value type and reference-counted type is much clearer than in Godot. Rc's only function is to reference-count a value, it's even in the name. For something like VariantArray or Dictionary, it's more an implementation detail due to how Godot works, and it wouldn't surprise me if some users expected them to behave like standard collection types (i.e. values).
So we make a choice by being consistent with either smart pointer types, or with collection types. Dictionary and friends seem to me closer to collections than to smart pointers.
In the Rc documentation, it is even recommended to use the associated function call syntax, to make it clearer that no deep copy is performed:

This indicates to me that the Rc::clone() method is indeed considered a special case, and the x.clone() syntax generally means "copy the whole contents of x".