Skip to content

Instantly share code, notes, and snippets.

@Xliff
Created October 12, 2022 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/69b7bccf1f740d61bcf91de27abe8657 to your computer and use it in GitHub Desktop.
Save Xliff/69b7bccf1f740d61bcf91de27abe8657 to your computer and use it in GitHub Desktop.
On Named Parameter Aliases...

Have I told you how wonderful I think Raku is?

Well I don't have enough words to do that.

However even the best languages have thier faults, and one of Raku's the following:

sub a ( :name(:named(:$names)) ) { ... }

The above describes a sub thats has a single named parameter with two aliases. That's a bit hard to read.

Now I tried to do something different based on inspiration from lizmat's Method::Also:

role AttributeAlso[@aliases] {

  method named_names {
    |@aliases.reverse, self.name.substr(1);
  }

}

multi sub trait_mod:<is>(Parameter:D \param, :$also!) is export {
  say "Aliasing { param.^name }";
  param does AttributeAlso[$also]
}

Which does what it looks to do...

$raku -Ilib -e 'use Method::Also; class A { method a (:$names is also<name named>) { &?ROUTINE.signature.params.skip(1).head.named_names.gist.say; $names.say } }; A.a(named=>1)'
Aliasing Parameter
(named name names)
(Any)

However, setting the aliases in the meta-model doesn't appear to be enough to actually set the value.

Can anyone tell me what I'm missing?

@lizmat
Copy link

lizmat commented Oct 12, 2022

Looks like named_names is only informational. I guess you'd have to look in the grammar / actions to see how such named parameters are actually set up / codegenned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment