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

Can't sort objects of a custom class, can't supply .cmp method in Rakudo #1435

Closed
p6rt opened this issue Dec 14, 2009 · 6 comments
Closed

Can't sort objects of a custom class, can't supply .cmp method in Rakudo #1435

p6rt opened this issue Dec 14, 2009 · 6 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Dec 14, 2009

Migrated from rt.perl.org#71258 (status was 'resolved')

Searchable as RT71258$

@p6rt
Copy link
Author

p6rt commented Dec 14, 2009

From @masak

In Rakudo 7ef386​:

$ perl6 -e 'class A {}; say (A.new, A.new).sort'
Multiple Dispatch​: No suitable candidate found for 'cmp', with signature 'PP->I'
[...]

$ perl6 -e 'class A {}; multi sub cmp(A $a, A $b) { 1 }; say (A.new,
A.new).sort'
Multiple Dispatch​: No suitable candidate found for 'cmp', with signature 'PP->I'
[...]

I'm not sure the end user should ever see that ungainly
error-with-Parrot-signature. It's also unfortunate that there doesn't
seem to be a way to make it go away for user-defined classes.

@p6rt
Copy link
Author

p6rt commented Jul 27, 2010

From @bbkr

[18​:12] <bbkr> rakudo​: class A {}; say (A.new, A.new).sort # is this
correct output?
[18​:12] <p6eval> rakudo 9808d7​: OUTPUT«A()<0x82b09f0>A()<0x82b1170>␤»
[18​:13] --> eternaleye has joined this channel
(~quassel@​unaffiliated/eternaleye).
[18​:13] <masak> bbkr​: what would you expect it to sort on?
[18​:14] <PerlJam> bbkr​: it looks correct to me 0x82b09f0 comes before
0x82b1170 :)
[18​:15] <bbkr> masak​: IMO this is correct - sort used "~A.new" output,
but I want to be sure before taking
http://rt.perl.org/rt3/Ticket/Display.html?id=71258 for tests
[18​:15] <masak> bbkr​: looking.
[18​:16] <masak> bbkr​: the ticket has two parts. the above shows that the
first part is fixed.
[18​:16] --> justatheory has joined this channel
(~justatheo@​c-24-21-193-250.hsd1.or.comcast.net).
[18​:17] <bbkr> masak​: second part works also
[18​:17] <bbkr> rakudo​:
[18​:17] <bbkr> class A {}; multi sub cmp(A $a, A $b) { 1 }; say (A.new,
[18​:17] <bbkr> A.new).sort
[18​:17] <p6eval> rakudo 9808d7​: ( no output )
[18​:17] <bbkr> rakudo​: class A {}; multi sub cmp(A $a, A $b) { 1 }; say
(A.new, A.new).sort
[18​:17] <p6eval> rakudo 9808d7​: OUTPUT«A()<0x69f1460>A()<0x6a1d7a0>␤»
[18​:19] <masak> rakudo​: class A { has $.x }; multi sub cmp(A $a, A $b) {
$a.x <=> $b.x }; say (A.new(​:x(2)), A.new(​:x(3)), A.new(​:x(1))).sort
[18​:19] <p6eval> rakudo 9808d7​:
OUTPUT«A()<0x73823e0>A()<0x7384300>A()<0x73861e0>␤»
[18​:19] <masak> er :)
[18​:19] <masak> rakudo​: class A { has $.x; method Str { $.x } }; multi
sub cmp(A $a, A $b) { $a.x <=> $b.x }; say (A.new(​:x(2)), A.new(​:x(3)),
A.new(​:x(1))).sort
[18​:19] <p6eval> rakudo 9808d7​: OUTPUT«123␤»
[18​:19] <masak> \o/
[18​:20] <masak> bbkr​: yes. it should be taken for tests.
[18​:20] <pugssvn> r31847 | Kodi++ | [t/spec] Fixed some Instant and
Duration tests and fudged some for Rakudo.
[18​:21] <bbkr> BTW​: how to get class instance memory address that can be
assigned to variable?
[18​:21] <jnthn> .WHERE
[18​:21] <bbkr> rakudo​: class A{}; A.new.WHERE.say
[18​:21] <p6eval> rakudo 9808d7​: OUTPUT«130402704␤»
[18​:22] <bbkr> jnthn​: thanks!

taken for tests

@p6rt
Copy link
Author

p6rt commented Jul 27, 2010

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

@p6rt
Copy link
Author

p6rt commented Jul 30, 2010

From @bbkr

rechecked on * release, also works

tests in
t/spec/S32-list/sort.t

@p6rt
Copy link
Author

p6rt commented Jul 30, 2010

@bbkr - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Jul 30, 2010
@p6rt
Copy link
Author

p6rt commented Jul 30, 2010

From @kyleha

This is an automatically generated mail to inform you that tests are now available in t/spec/S32-list/sort.t

commit 307c1ab2718b1bb94c287d2b3ce1c365753f362a
Author​: bbkr <bbkr@​c213334d-75ef-0310-aa23-eaa082d1ae64>
Date​: Fri Jul 30 16​:57​:13 2010 +0000

  [t/spec] tests for RT #​71258 Cant sort objects of a custom class, cant supply .cmp method in Rakudo
 
  git-svn-id​: http://svn.pugscode.org/pugs@&#8203;31870 c213334d-75ef-0310-aa23-eaa082d1ae64

Inline Patch
diff --git a/t/spec/S32-list/sort.t b/t/spec/S32-list/sort.t
index 1e0d219..27205a2 100644
--- a/t/spec/S32-list/sort.t
+++ b/t/spec/S32-list/sort.t
@@ -1,6 +1,6 @@
 use v6;
 use Test;
-plan 31;
+plan 35;
 
 # L<S32::Containers/"List"/"=item sort">
 
@@ -220,4 +220,37 @@ plan 31;
         'sort accepts rand method';
 }
 
+# RT #71258
+{
+    class RT71258_1 { };
+
+    my @sorted;
+
+    lives_ok { @sorted = (RT71258_1.new, RT71258_1.new).sort },
+        'sorting by stringified class instance (name and memory address)';
+
+    ok ([<]@sorted.map({.WHERE})),
+        'checking sort order by class memory address';
+
+    class RT71258_2 {
+        has $.x;
+        method Str { $.x }
+    };
+
+    multi sub cmp(RT71258_2 $a, RT71258_2 $b) {
+        $a.x <=> $b.x;
+    }
+
+    lives_ok {
+        @sorted = (
+            RT71258_2.new(x => 2),
+            RT71258_2.new(x => 3),
+            RT71258_2.new(x => 1)
+        ).sort
+    }, 'sorting stringified class instance with custom cmp';
+
+    is ~@sorted, '1 2 3',
+        'checking sort order with custom cmp';
+}
+
 # vim: ft=perl6

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

No branches or pull requests

1 participant