Rules / Types, classes & roles / basic

Variable Reference

Object representation of a variable for use in traits

What it is #

Routines #

Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.

trait is #

multi trait_mod:<is>(Variable:D, :$dynamic)

Marks a variable as dynamic, that is, accessible from inner dynamic scopes without being in an inner lexical scope. The is dynamic trait is a rather cumbersome way of creating and accessing dynamic variables. A much easier way is to use the * twigil:

trait of #

multi trait_mod:<of>(Mu:U $target, Mu:U $type)

Sets the type constraint of a container bound to a variable. You can use any value defined in compile time as a type constraint, including constants: which would be equivalent to the previous definition.

method name #

method name(Variable:D: str)

Returns the name of the variable, including the sigil.

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

3 no-output · 1 not-runnable · 2 ok · 1 rakupp-differs

class Variable {}

Not executed: the documentation states no expected output for this example.

my Int $x is default(42);
say $x;     # OUTPUT: «42␤»
$x = 5;
say $x;     # OUTPUT: «5␤»
# explicit reset:
$x = Nil;
say $x;     # OUTPUT: «42␤»
Output
42
5
42

Documentation, Rakudo and Raku++ all agree.

my @array is default( 'N/A' );
@array[22].say;  # OUTPUT: N/A
@array = Nil;
@array.say;      # OUTPUT: [N/A]
@array[4].say;   # OUTPUT: N/A

my %hash is default( 'no-value-here' );
%hash<non-existent-key>.say; # OUTPUT: no-value-here
%hash<foo> = 'bar';
%hash<>.say;                 # OUTPUT: {foo => bar}
%hash<wrong-key>.say;        # OUTPUT: no-value-here

Not executed: the documentation states no expected output for this example.

sub introspect() {
say $CALLER::x;
}
{
my $x is dynamic = 42;
introspect();  # OUTPUT: «42␤»
}
{
my $x = 41;    # not dynamic
introspect();  # dies with an exception of X::Caller::NotDynamic
}

Neither engine can run this in isolation — the example depends on context from the surrounding text.

sub introspect() {
say $*x;
}
{
my $*x = 42;
introspect();  # OUTPUT: «42␤»
}
{
my $x = 41;    # not dynamic
introspect();  # dies with an exception of X::Dynamic::NotFound
}
Documentation
42
Rakudo
42
Raku++
42
(Any)

Raku++ disagrees with both the documentation and Rakudo — a defect.

my $i of Int = 42;
$i = "forty plus two";
CATCH { default { say .^name, ' ', .Str } }
# OUTPUT: «X::TypeCheck::Assignment Type check failed in assignment to $i; expected Int but got Str ("forty plus two")␤»
Output
X::TypeCheck::Assignment Type check failed in assignment to $i; expected Int but got Str ("forty plus two")

Documentation, Rakudo and Raku++ all agree.

constant \T = Int;
my $i of T = 42;

Not executed: the documentation states no expected output for this example.