Cloning only when necessary

R

Robert Klemme

Hi,

if you want to make sure you get a copy of an instance that nobody
else has access to you can #clone it. That's not necessary if the
instance is immutable or frozen (or both). Does anybody think it is a
good idea to implement this? E.g.

class Object
def frown
frozen? ? self : clone
end
end

class Numeric
def frown
self
end
end

...

The you can do in constructor code for example

def initialize(something)
@x = something.frown
end

and be sure that you do not face aliasing issues with @x. Does
anybody think we should have this in the language?

Note: The name is remotely inspired by "FReeze" and "OWN". I'm not
religious about it, not at all. It's just that I could not make up a
better one. Suggestions welcome.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top