Better than java, and not my choice unless I want to look for another job?
But I exaggerated: I don’t care that the compiler typechecks the implementation of the maybe-copy-interface (that’s like 5 lines of code), but I care very much that the compiler type-checks uses of the interface (which are by different people and all over the place).

Not sure what “UB” means? Unacceptable Behaviour maybe? If so, what does that means?
“Undefined behavior”. In e.g. C,
extern void bar(int);
int foo(){
for(int i = 1<<30;; i++) {
if(i < 0) return -1;
bar(i);
}
return 0;
}
the function bar
will of course by called with negative arguments, because signed integer overflow is undefined in C – meaning the compiler can assume that it does not happen (“what may not be, cannot be”), and can use that knowledge to optimize out my careful runtime check. If the axioms of the compiler are violated, you get the usual “ex falso quod libet” consequence that your entire logical edifice crashes down. UB is the technical term for that.
@sangamon so your proposal is ultimately equivalent to my previous implicit-based approach? I.e. I have some dynamically dispatched function that performs the right clone-implementation (either identity or make a copy and wire external refs), and then do a .asInstanceOf
cast?
But I still don’t understand whether this.type
as a return type means “returns the same type” or means “returns the same instance”. Is final class Foo{def copyMe:this.type =null}
valid? Is final class Foo{def copyMe:this.type = (new Foo).asInstanceOf[this.type]}
valid?