Skip to content

Clarify clone, new_ref, duplicate and duplicate_deep semantics #712

Open
@Bromeon

Description

@Bromeon

From #711:

@Bromeon:

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 clone and duplicate[_deep] or is the idea "when it supports both shallow and deep copy, use duplicate/duplicate_deep, otherwise use clone?

@toasteater:

I'm aware that it's a bit messy right now. For clone vs duplicate, my understanding is that Clone on a reference-counted type should be a reference increment (i.e. not even a shallow copy), to be consistent with standard types like Rc, Arc, even though the reference-counting is "internal" in the case of variant collections. That does make NewRef seem 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:
grafik

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking-changeIssues and PRs that are breaking to fix/merge.c: coreComponent: core (mod core_types, object, log, init, ...)documentationquality-of-lifeNo new functionality, but improves ergonomics/internals

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions