Course of Raku / Essentials / More about functions

Typed parameters

Function parameters in Raku can be made type restricted. This is very similar to typed variables.

sub add(Int $x, Int $y) { $x + $y }

The function now demands its arguments to be integer numbers.

say add(10, 20);
# say add(pi, e); # Error

An attempt to pass a parameter of any other type than Int is a compile-time error.

$ raku t.raku
===SORRY!=== Error while compiling t.raku
Calling add(Num, Num) will never work with declared signature (Int $x, Int $y)
at t.raku:5
------> say ⏏add(pi, e);

Notice that Raku won’t automatically convert types even if it is possible in other cases.

# say add('3', '4'); # Error
say '3' + '4'; # OK and is 7

How to tell if it’s a compile-time error

If the error message starts with ===SORRY!=== Error while compiling, it means that the error happened at compile time.


Complete the quiz that covers the contents of this topic.

Course navigation

More about functions / Mind the space   |   More about functions / Return type

💪 Or jump directly to the exercise to this section.