How can I reference an existing method in Raku?

81 views Asked by At

As explained in the docs, you can reference an existing function by prepending the & sigil:

&say       # reference to the `say` function
&infix:<+> # reference to the infix `+` operator

How do I do this for methods?

1

There are 1 answers

1
uzluisf On

This article's author explains there are a few possibilities to get hold of a method object, however the most relevant one here is to use the MOP method .^lookup:

say Array.^lookup('push').raku

#`«
proto method push ($: |) {*}
»

For example to get all the candidates's signatures for Array's push method you'd do:

.say for Array.^lookup('push').candidates.map(*.signature);

#`«
(Any:U \SELF: |values)
(Array:D: Slip \value, *%_ --> Array:D)
(Array:D: \value, *%_ --> Array:D)
(Array:D: **@values is raw, *%_ --> Array:D)
»