Course of Raku / Advanced / Subroutines / More on MAIN subroutines / Exercises / A typed MAIN

Solution: A typed MAIN

Here is a possible solution to the task.

Code

sub MAIN(Int $n) {
    say $n * 2;
}

🦋 You can find the source code in the file typed-main.raku.

Output

$ raku typed-main.raku 5
10

$ raku typed-main.raku abc
Usage:
  typed-main.raku <n>

Comments

  1. The Int constraint means the command-line word must look like an integer. Given 5, it binds to $n and the body prints 10.

  2. Given abc, the value cannot become an Int, so the signature does not match. Raku does not run the body — it prints the generated usage message instead, giving you argument validation without any manual checks.

Course navigation

A typed MAIN   |   Add two arguments