Skip Menu |
Report information
Id: 127793
Status: open
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: cookbook_000 [at] yahoo.co.jp <titsuki [at] cpan.org>
Cc:
AdminCc:

Severity: (no value)
Tag: (no value)
Platform: (no value)
Patch Status: (no value)
VM: (no value)



Subject: The cmp operator for user-defined classes lacks consistency
Download (untitled) / with headers
text/plain 606b
It seems that the cmp operator for user-defined classes lacks consistency. See the following commands. $ perl6 -e 'class MyClass {}; my $foo = MyClass.new(); my $bar = MyClass.new(); ($foo cmp $bar).perl.say' Order::Less $ perl6 -e 'class MyClass {}; my $foo = MyClass.new(); my $bar = MyClass.new(); ($bar cmp $foo).perl.say' Order::More $ perl6 -e '(Str.new cmp Str.new).perl.say' Order::Same Comparing two instances of the type object Str seems to check its type, but comparing that of MyClass seems to check its address or something. I think that it should return Order::Same or something Exception.
RT-Send-CC: perl5-porters [...] perl.org
Download (untitled) / with headers
text/plain 825b
Sorry, it is the post for perl6. I mistook the category of this post. But I don't have a permission to change that. On 2016-3月-27 日 07:05:53, cookbook_000@yahoo.co.jp wrote: Show quoted text
> It seems that the cmp operator for user-defined classes lacks > consistency. > See the following commands. > > $ perl6 -e 'class MyClass {}; my $foo = MyClass.new(); my $bar = > MyClass.new(); ($foo cmp $bar).perl.say' > Order::Less > $ perl6 -e 'class MyClass {}; my $foo = MyClass.new(); my $bar = > MyClass.new(); ($bar cmp $foo).perl.say' > Order::More > $ perl6 -e '(Str.new cmp Str.new).perl.say' > Order::Same > > Comparing two instances of the type object Str seems to check its > type, but comparing that of MyClass seems to check its address or > something. > > I think that it should return Order::Same or something Exception.
Download (untitled) / with headers
text/plain 222b
On Sun Mar 27 07:11:14 2016, cookbook_000@yahoo.co.jp wrote: Show quoted text
> Sorry, it is the post for perl6. > I mistook the category of this post. > But I don't have a permission to change that.
Looks like I do, I've moved it. Tony
To: "Itsuki Toyota (via RT)" <perlbug-followup [...] perl.org>
From: Elizabeth Mattijsen <liz [...] dijkmat.nl>
Date: Mon, 28 Mar 2016 17:02:29 +0200
Subject: Re: [perl #127793] The cmp operator for user-defined classes lacks consistency
Download (untitled) / with headers
text/plain 1.3k
Show quoted text
> On 27 Mar 2016, at 16:05, Itsuki Toyota (via RT) <perlbug-followup@perl.org> wrote: > > # New Ticket Created by Itsuki Toyota > # Please include the string: [perl #127793] > # in the subject line of all future correspondence about this issue. > # <URL: https://rt.perl.org/Ticket/Display.html?id=127793 > > > > It seems that the cmp operator for user-defined classes lacks consistency. > See the following commands. > > $ perl6 -e 'class MyClass {}; my $foo = MyClass.new(); my $bar = MyClass.new(); ($foo cmp $bar).perl.say' > Order::Less > $ perl6 -e 'class MyClass {}; my $foo = MyClass.new(); my $bar = MyClass.new(); ($bar cmp $foo).perl.say' > Order::More > $ perl6 -e '(Str.new cmp Str.new).perl.say' > Order::Same > > Comparing two instances of the type object Str seems to check its type, but comparing that of MyClass seems to check its address or something. > > I think that it should return Order::Same or something Exception.
The default cmp calls .Stringy on the objects, so effectively the comparison becomes something like "MyClass<123345>" cmp "MyClass<234556>”. I guess we *could* introspect the object for public attributes and start cmp-ing them (if they are of the same type, of course). But I fear for the level of DWIM: it looks like becoming a WAT very fast. Perhaps we should only check for address equality, and if not, return Nil (indicating we don’t know).
Download (untitled) / with headers
text/plain 1.6k
Thank you for letting me know the mechanism of the cmp operator. Returning Nil seems better in my personal view, because there is no sense comparing Stringified user-defined classes. On 2016-3月-28 月 08:03:09, elizabeth wrote: Show quoted text
> > On 27 Mar 2016, at 16:05, Itsuki Toyota (via RT) <perlbug- > > followup@perl.org> wrote: > > > > # New Ticket Created by Itsuki Toyota > > # Please include the string: [perl #127793] > > # in the subject line of all future correspondence about this issue. > > # <URL: https://rt.perl.org/Ticket/Display.html?id=127793 > > > > > > > It seems that the cmp operator for user-defined classes lacks > > consistency. > > See the following commands. > > > > $ perl6 -e 'class MyClass {}; my $foo = MyClass.new(); my $bar = > > MyClass.new(); ($foo cmp $bar).perl.say' > > Order::Less > > $ perl6 -e 'class MyClass {}; my $foo = MyClass.new(); my $bar = > > MyClass.new(); ($bar cmp $foo).perl.say' > > Order::More > > $ perl6 -e '(Str.new cmp Str.new).perl.say' > > Order::Same > > > > Comparing two instances of the type object Str seems to check its > > type, but comparing that of MyClass seems to check its address or > > something. > > > > I think that it should return Order::Same or something Exception.
> > The default cmp calls .Stringy on the objects, so effectively the > comparison becomes something like "MyClass<123345>" cmp > "MyClass<234556>”. > > I guess we *could* introspect the object for public attributes and > start cmp-ing them (if they are of the same type, of course). But I > fear for the level of DWIM: it looks like becoming a WAT very fast. > > Perhaps we should only check for address equality, and if not, return > Nil (indicating we don’t know).


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org