I’m generally supportive of this proposal. However, I’m hesitant about the method name clone().
While I understand the motivation to align with a potential future Clonable protocol, adopting this name now feels like it sets a specific precedent for a feature that hasn't been fully explored yet.
I have a few concerns with stabilizing clone() at this stage:
- Naming Flexibility: We might eventually decide that
Clonableisn't the right name for the protocol (e.g., we might preferExplicitlyCopyableor something entirely different) or that the method should be named differently. Usingclone()here leans heavily in one direction before that debate has happened. - Namespace Pollution: If
Copyableeventually refines this future protocol (as suggested in Future Directions), we risk a future where every copyable type gains a redundant.clone()method. This could clutter the namespace and clash with existing user-defined methods. We would likely need disambiguation rules, compiler quirks to hideclone()when conformance toCopyableis visible, or linters to catch redundant calls toclone(). - Previous discussions regarding explicit copying explored using a
copyoperator (copy x) to mirrorconsumeandborrow. If the language adopts acopyoperator for types conforming to this new protocol,Boxhaving a.clone()method would create an awkward inconsistency in vocabulary.
I'd suggest we stick to the terminology we have today - "copy" - either as an initializer or a method.
extension Box where Value: Copyable {
public init(copying source: borrowing Box<Value>)
// or
public func copy() -> Box<Value>
}
Personally, I prefer the initializer approach. If Swift adopts a Deref-like mechanism (as mentioned in Future Directions), Box having a method named copy() creates potential ambiguity if the wrapped value also has a copy() method. An initializer avoids this collision entirely.