[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Oppinions on RCR for dup on immutable classes

Yukihiro Matsumoto

2/16/2007 6:55:00 AM

Hi,

In message "Re: Oppinions on RCR for dup on immutable classes"
on Fri, 16 Feb 2007 08:55:12 +0900, "Phrogz" <gavin@refinery.com> writes:

|That's a statement of fact, but doesn't explain *why* it's a problem,
|or (important for an RCR) why it needs to be fixed in the core of the
|language.
|
|What is the use case that is prevented by the problem? What does it
|make inconvenient? Why should it be changed?

Seconded. It pretty trivial for us core developers to make dup for
immutable objects to return themselves, but _I_ don't understand why
it is needed. I assume obj.object_id != obj.dup.object_id, and see no
good reason enough to break the assumption.

matz.


29 Answers

Gregory Brown

2/16/2007 2:43:00 PM

0

On 2/16/07, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> Hi,
>
> In message "Re: Oppinions on RCR for dup on immutable classes"
> on Fri, 16 Feb 2007 08:55:12 +0900, "Phrogz" <gavin@refinery.com> writes:
>
> |That's a statement of fact, but doesn't explain *why* it's a problem,
> |or (important for an RCR) why it needs to be fixed in the core of the
> |language.
> |
> |What is the use case that is prevented by the problem? What does it
> |make inconvenient? Why should it be changed?
>
> Seconded. It pretty trivial for us core developers to make dup for
> immutable objects to return themselves, but _I_ don't understand why
> it is needed. I assume obj.object_id != obj.dup.object_id, and see no
> good reason enough to break the assumption.

+1. If i am trying to dup an object, sometimes it's inconvenient that
it complains,
but most of the time i want to know about it.

I also don't think something like 5.dup has semantic meaning.

Ara.T.Howard

2/16/2007 3:22:00 PM

0

Stefan Rusterholz

2/16/2007 4:45:00 PM

0

Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: Oppinions on RCR for dup on immutable classes"
> on Fri, 16 Feb 2007 08:55:12 +0900, "Phrogz" <gavin@refinery.com>
> writes:
>
> |That's a statement of fact, but doesn't explain *why* it's a problem,
> |or (important for an RCR) why it needs to be fixed in the core of the
> |language.
> |
> |What is the use case that is prevented by the problem? What does it
> |make inconvenient? Why should it be changed?
>
> Seconded. It pretty trivial for us core developers to make dup for
> immutable objects to return themselves, but _I_ don't understand why
> it is needed. I assume obj.object_id != obj.dup.object_id, and see no
> good reason enough to break the assumption.
>
> matz.

That's why I have 2 suggestions. The issue arises when you don't know
what you dup.
Under the aspect that obj.object_id should always be ==
obj.dup.object_id the first suggestion
of removing dup from those classes would be more appropriate.
To me it seems inconsequential to implement a method with it only
raising an Exception.
That makes it impossible to check if your object doesn't actually
implement dup (e.g. via
respond_to?). To me it seems that it should either be an implementation
detail that
those classes can't really be duped (ie. return self) or expose that
transparently (not implement dup).
That of course is my personal oppinion and I'm posting this on the ML to
see if it is only me :)

My regards.

--
Posted via http://www.ruby-....

Gregory Brown

2/16/2007 5:41:00 PM

0

On 2/16/07, Stefan Rusterholz <apeiros@gmx.net> wrote:
> Yukihiro Matsumoto wrote:
> > Hi,
> >
> > In message "Re: Oppinions on RCR for dup on immutable classes"
> > on Fri, 16 Feb 2007 08:55:12 +0900, "Phrogz" <gavin@refinery.com>
> > writes:
> >
> > |That's a statement of fact, but doesn't explain *why* it's a problem,
> > |or (important for an RCR) why it needs to be fixed in the core of the
> > |language.
> > |
> > |What is the use case that is prevented by the problem? What does it
> > |make inconvenient? Why should it be changed?
> >
> > Seconded. It pretty trivial for us core developers to make dup for
> > immutable objects to return themselves, but _I_ don't understand why
> > it is needed. I assume obj.object_id != obj.dup.object_id, and see no
> > good reason enough to break the assumption.
> >
> > matz.
>
> That's why I have 2 suggestions. The issue arises when you don't know
> what you dup.
> Under the aspect that obj.object_id should always be ==
> obj.dup.object_id the first suggestion
> of removing dup from those classes would be more appropriate.
> To me it seems inconsequential to implement a method with it only
> raising an Exception.
> That makes it impossible to check if your object doesn't actually
> implement dup (e.g. via
> respond_to?). To me it seems that it should either be an implementation
> detail that
> those classes can't really be duped (ie. return self) or expose that
> transparently (not implement dup).

>> a = 3
=> 3
>> b = a.dup rescue a
=> 3
>> b
=> 3

What's so bad about that?

Robert Dober

2/16/2007 6:43:00 PM

0

On 2/16/07, Gregory Brown <gregory.t.brown@gmail.com> wrote:
> On 2/16/07, Stefan Rusterholz <apeiros@gmx.net> wrote:
> > Yukihiro Matsumoto wrote:
> > > Hi,
> > >
> > > In message "Re: Oppinions on RCR for dup on immutable classes"
> > > on Fri, 16 Feb 2007 08:55:12 +0900, "Phrogz" <gavin@refinery.com>
> > > writes:
> > >
> > > |That's a statement of fact, but doesn't explain *why* it's a problem,
> > > |or (important for an RCR) why it needs to be fixed in the core of the
> > > |language.
> > > |
> > > |What is the use case that is prevented by the problem? What does it
> > > |make inconvenient? Why should it be changed?
> > >
> > > Seconded. It pretty trivial for us core developers to make dup for
> > > immutable objects to return themselves, but _I_ don't understand why
> > > it is needed. I assume obj.object_id != obj.dup.object_id, and see no
> > > good reason enough to break the assumption.
> > >
> > > matz.
> >
> > That's why I have 2 suggestions. The issue arises when you don't know
> > what you dup.
> > Under the aspect that obj.object_id should always be ==
> > obj.dup.object_id the first suggestion
> > of removing dup from those classes would be more appropriate.
> > To me it seems inconsequential to implement a method with it only
> > raising an Exception.
> > That makes it impossible to check if your object doesn't actually
> > implement dup (e.g. via
> > respond_to?). To me it seems that it should either be an implementation
> > detail that
> > those classes can't really be duped (ie. return self) or expose that
> > transparently (not implement dup).
>
> >> a = 3
> => 3
> >> b = a.dup rescue a
> => 3
> >> b
> => 3
>
> What's so bad about that?
>
>

I would ask the question: Why do you dup a?
The answer is, I presume, because I will modify the duplicate and I
do not want to modify the original.
ok so let us follow our code

b= a.dup rescue a
...

b.mutating_method
what will happen now?

Cheers
Robert

--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous

Gavin Kistner

2/16/2007 6:48:00 PM

0

From: SonOfLilit [mailto:sonoflilit@gmail.com]
Sent: Friday, February 16, 2007 10:48 AM
> Ruby is always about principle-of-least-surprise and this behavior
> really surprised me when I first read about it non-list (I've stayed
> away from object copies in MY code, because it all seems so hacky and
> arcane to me... Truns out it was a good idea)

Actually, Ruby is about the principle-of-least-surprise-for-matz.

I used to think POLS applied to everyone when I started with Ruby.
Many people, including Matz, pointed out that it's impossible to make
things unsurprising to everyone, because people will be surprised by
different things.

Just because something is surprising to you doesn't mean that it
should be changed in Ruby. You are advised (as was I) not to use POLS
as justification for why something should be changed.

....

Having said that, I personally would prefer for 3.dup to 'just work',
returning an object that is equivalent to the original. Just because
the new instance happens to be the same doesn't mean that it's bad -
as an immutable object, the only way to tell is via object_id, anyhow.

Gavin Kistner

2/16/2007 6:52:00 PM

0

On Feb 16, 11:43 am, "Robert Dober" <robert.do...@gmail.com> wrote:
> On 2/16/07, Gregory Brown <gregory.t.br...@gmail.com> wrote:
> > >> a = 3
> > => 3
> > >> b = a.dup rescue a
> > => 3
> > >> b
> > => 3
>
> > What's so bad about that?
>
> I would ask the question: Why do you dup a?
> The answer is, I presume, because I will modify the duplicate and I
> do not want to modify the original.
> ok so let us follow our code
>
> b= a.dup rescue a
> ..
>
> b.mutating_method
> what will happen now?

I'm not sure what your point is, Robert. Is it:

a) See, there's no point in duplicating an immutable object, since the
only reason to #dup is so you can mutate the instance later, and
anything you tried to do to mutate it would fail later on.

or is it

b) You should never ever write "a.dup rescue a" because it will give
you strange problems if a might refer to a mutable object that doesn't
respond to #dup.

?

dblack

2/16/2007 6:54:00 PM

0

Gavin Kistner

2/16/2007 7:12:00 PM

0

On Feb 16, 11:53 am, dbl...@wobblini.net wrote:
> On Sat, 17 Feb 2007, Phrogz wrote:
> > ...
> > Having said that, I personally would prefer for 3.dup to 'just work',
> > returning an object that is equivalent to the original. Just because
> > the new instance happens to be the same doesn't mean that it's bad -
> > as an immutable object, the only way to tell is via object_id, anyhow.
>
> It doesn't mean it's bad, but it does mean that it isn't a duplicate
> :-) At least, I wouldn't think that dup is the right name for a
> method that might actually return the receiver.

My perspective is:
If a duplicate of an immutable object is indistinguishable from the
original, then you would have no idea if #dup was returning an
internal duplicate or the same instance. And you wouldn't care (except
that you'd probably prefer it to conserve resources internally and
return the same instance).

The determining question for me, then, is:
How indistinguishable would two distinct instances of these immutable
objects be? The only case where code might break is if you had:
b = a.dup
h = { a.object_id => 1, b.object_id=> 2 }
and you were very unhappy if you ended up running over the key. I've
never personally used object_id for that purpose (or any other than
debugging), so I'm not sure how likely that is. I just use objects
themselves as hash keys, in which case equivalent instances already
behave the same (even of mutable objects!):

irb(main):001:0> a = []; b=a.dup
=> []
irb(main):002:0> p a.object_id, b.object_id
23327420
23327410
irb(main):003:0> h = {a=>1, b=>2}
=> {[]=>2}

irb(main):013:0> a,b = 1234567890,1234567890
=> [1234567890, 1234567890]
irb(main):014:0> p a.class, a.object_id, b.class, b.object_id
Bignum
23346300
Bignum
23346270
=> nil
irb(main):015:0> h = {a=>1,b=>2}
=> {1234567890=>2}


Gary Wright

2/16/2007 7:25:00 PM

0


On Feb 16, 2007, at 1:50 PM, Phrogz wrote:
> Having said that, I personally would prefer for 3.dup to 'just work',
> returning an object that is equivalent to the original. Just because
> the new instance happens to be the same doesn't mean that it's bad -
> as an immutable object, the only way to tell is via object_id, anyhow.

I've come across this situation when writing generic code to do a
'deep copy'. Instead of changing the semantics of #dup, why not have
some other method that means 'make a copy if you can but if the object
has immutable semantics then return a reference to self'.

As for a name for such a method, how about Kernel#another ?

I'm not sure how #dup, #clone, and #another should be related. Perhaps
#another should use #clone instead of #dup? Maybe there should be
another
version of #another that uses clone semantics?

Gary Wright