Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The cmp operator for user-defined classes lacks consistency #5202

Open
p6rt opened this issue Mar 27, 2016 · 7 comments
Open

The cmp operator for user-defined classes lacks consistency #5202

p6rt opened this issue Mar 27, 2016 · 7 comments
Labels
RFC Request For Comments

Comments

@p6rt
Copy link

p6rt commented Mar 27, 2016

Migrated from rt.perl.org#127793 (status was 'open')

Searchable as RT127793$

@p6rt
Copy link
Author

p6rt commented Mar 27, 2016

From @titsuki

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.

@p6rt
Copy link
Author

p6rt commented Mar 27, 2016

From @titsuki

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​:

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.

@p6rt
Copy link
Author

p6rt commented Mar 27, 2016

From [Unknown Contact. See original ticket]

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​:

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.

@p6rt
Copy link
Author

p6rt commented Mar 27, 2016

From @tonycoz

On Sun Mar 27 07​:11​:14 2016, cookbook_000@​yahoo.co.jp wrote​:

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

@p6rt
Copy link
Author

p6rt commented Mar 27, 2016

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Mar 28, 2016

From @lizmat

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-archive.perl.org/perl6/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).

@p6rt
Copy link
Author

p6rt commented Mar 29, 2016

From @titsuki

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​:

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-archive.perl.org/perl6/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).

@p6rt p6rt added the RFC Request For Comments label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC Request For Comments
Projects
None yet
Development

No branches or pull requests

1 participant