๐Ÿ“˜ 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 *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code