Immutable view of class instance

I played with the idea of compiler-enforced value semantics earlier and wrote this exploratory proposal. I do believe the idea would work, but don't think it's very realistic to add this to the language.

To me, it sounds like the idea discussed at the end of this recent thread would work better. Inspired from NSCopying, add a Cloning protocol with a clone() method you can call every time you need a copy detached from any previous reference. Maybe allow the compiler to auto-generate the implementation of the clone() method if all the members conform to Cloning. And maybe add an @autocloning type modifier you could use to tell the compiler to call clone() every time a copy is made.

With all these features, you could have a signature like this:

struct Dictionary<@autocloning Key, Value> 
    where Key: Clonable, Hashable
{ ... }

and be sure keys won't mutate under your nose because clone() will be called automatically at every copy. (Assuming clone() is implemented properly.) Of course adding this to Dictionary would break source-compatibility.