📘 Typed arguments in Perl 6 subs

📘 Typed arguments in Raku subs

N. B. Perl 6 has been renamed to Raku. Click to read more.


Similarly to the above-described typed variables, it is possible to indicate that the sub’s parameters are typed. To do so, add a type name before the name of the parameter.

sub say-hi(Str $name) {
    say "Hi, $name!";
}

If the types of the expected and the actual parameters do not match, a compile-time error will occur.

say-hi("Mr. X"); # OK

# say-hi(123); # Error: Calling say-hi(Int) will never work
               # with declared signature (Str $name)

Leave a Reply

Your email address will not be published. Required fields are marked *