method set( :$x = $.x, :$y = $.y )
####
Named parameters
In contrast to positional parameters, named parameters are referred
by their names. The following function takes two parameters called
$from and $to:
sub distance(:$from, :$to) { $from - $to }
Now, to call the function, you need to name the arguments:
say distance(from => 30, to => 10); # 20
It is an error to pass the arguments as if they were positional.
For example, a call distance(30, 10) generates an error: ...
####
class Rectangle {
has Int $.length = 1;
has Int $.width = 1;
method area(--> Int) {
return $!length * $!width;
}
}
my $r1 = Rectangle.new(length => 2, width => 3);
say $r1.area();
####
has Bool $.done;
####
has Bool $!done;
method done() { return $!done }
####
use v6;
class Point {
has $.x = 0;
has $.y = 0;
method gist { "[$.x, $.y]" }
method set( :$joe = $!x, :$susan = $!y ) {
$!x = $joe;
$!y = $susan;
}
}
my $point = Point.new();
say $point.x; # 0
say $point; # [0, 0]
# $point.x = 3; Error!
$point = Point.new(x => 3, y => 5);
say $point; # [3,5]
$point.set(joe => 10, susan => 20);
say $point; # [10, 20]
$point.set(joe => 100);
say $point; # [100, 20]