Course of Raku / Essentials

Typed variables

In Raku, a scalar variable (or, a scalar container) can keep a single object of different types. For example, the same variable can first contain a number, and then a string:

my $var = 42;
$var = 'Hello';

Here is another example of mixing different data types in the same expression:

my $a = '100';
my $b = 200;
say $a + $b; # 300

This is, probably, not the best coding practice, but it is a perfectly valid Raku program.

Nevertheless, Raku allows you to specify what a given variable may keep. There are more details in the below topics.

Exercises

This section contains 4 exercises. Examine all the topics of this section before going to the coding practice.

  1. Create variables of all known types
  2. Examine the result of prompt — Strings
  3. Examine the result of prompt — Numbers
  4. Converting user input to a number

Course navigation

Data type conversion / Converting types with prefix operators   |   Type constraints