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

trait_mod:<is>(Routine:D, |) is called on the proto, not the candidate #5289

Open
gfldex opened this issue Jun 19, 2023 · 2 comments
Open

trait_mod:<is>(Routine:D, |) is called on the proto, not the candidate #5289

gfldex opened this issue Jun 19, 2023 · 2 comments

Comments

@gfldex
Copy link
Contributor

gfldex commented Jun 19, 2023

For a multi trait_mod:<is>(Routine:D, |) is always called on the proto, never on the actual candidate. This has been deemed wrong in the discussion: https://irclogs.raku.org/raku-dev/2023-06-05.html

    multi sub trait_mod:<is>(Routine \r, :$renamed) { r.candidates.say; r does role :: { method name { $renamed } } }

    proto sub foo(|) is renamed<foo-proto> { * }
    multi sub foo(Int $) { say &?ROUTINE.name }
    multi sub foo(Str $) is renamed<stringer> { say &?ROUTINE.name }
    multi sub foo(Failure) is renamed<failer> { fail('oi‽') }

    foo(42);
    foo('answer');
    # foo(Failure);

Expected Output

foo
stringer

Actual Output

foo
failer

Environment

  • Operating system: Linux dexhome 6.1.0-9-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.27-1 (2023-05-08) x86_64 GNU/Linux
  • Compiler version (perl6 -v or raku -v): Welcome to Rakudo™ v2023.05-98-ge8b477b58. Implementing the Raku® Programming Language v6.d. Built on MoarVM version 2023.05.
@jnthn
Copy link
Member

jnthn commented Jun 19, 2023

It's called on the candidate, but roles are not closures (and ideally we'll find a way to make this spit out a warning in the new compiler frontend), so $renamed in the method always references the value in the most recent call of the trait.

@gfldex
Copy link
Contributor Author

gfldex commented Jun 21, 2023

Indeed, I stepped into the "not quite golfed enough"-trap.

    multi sub trait_mod:<is>(Routine \r, :$thiscalled) { dd r; }

    proto sub foo(|) is thiscalled<?> { }
    multi sub foo(Str $s, :$isa = <stringer>) is thiscalled<?> { }
    multi sub foo(Int $s, :$isa = <inter>) is thiscalled<?> { }

OUTPUT:

Sub foo = proto sub foo (|) { #`(Sub|5551856403328) ... }
Sub foo = multi sub foo (Str $s, :$isa = "stringer") { #`(Sub|5551856429696) ... }
Sub foo = multi sub foo (Int $s, :$isa = "inter") { #`(Sub|5551856430848) ... }

Do we have an issue-tag for "needs a warning"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants