Raku By Example
View me onGitHub
## multi MAIN

multi sub MAIN('send', $filename) {
 ...
}
multi sub MAIN('fetch', $filename) {
 ...
}
multi sub MAIN('compare', $file1, $file2) {
 ...
}

#`(
... 是 yadayadayada 占位符
> perl6 "multiple_MAIN.p6"
Usage:
  multiple _MAIN.p6 send <filename>
  multiple _MAIN.p6 fetch <filename>
  multiple _MAIN.p6 compare <file1> <file2>
)

## multi postcircumfix for string slice

multi postcircumfix:<[ ]> ( Str:D $s, Int:D $n --> Str ) {
	$s.substr: $n, 1
	}
multi postcircumfix:<[ ]> ( Str:D $s, Range:D $r --> Str ) {
	$s.substr: $r.min, $r.max - $r.min + 1
	}
multi postcircumfix:<[ ]> ( Str:D $s, List:D $i --> List ) {
	map( { $s.substr: $_, 1 }, @$i ).List
	}

my $string = 'The quick, purple butterfly';

{ # Works
my $single = $string[0];
say $single; # T
}

{ # Works
my $substring = $string[5..9];
say $substring; # uick
}

{ # Works
my $substring = $string[1,3,5,7];
say $substring; # (h   u c)
}

## multi with proto

proto mνlti(|) {*};

multi mνlti(1) { "Yep"};
multi mνlti(2) { "Nay"};
multi mνlti(Int $) { "Welll.."};

say mνlti($_) for ^10;

## multi with signature

#|(Some enlightening words about myfunc.)
sub my-single-func (Int $i) { say "You provided an integer: $i"; };
#=(Some more words about myfunc.)

say "my-single-func is a {&my-single-func.perl}";
say "This {&my-single-func.^name} does {&my-single-func.WHY}";


#|(my-multi-func accepts either an integer or a string)
proto my-multi-func (|) {*}

#|(myfunc accepts an integer.)
multi my-multi-func (Int $i) { say "You provided an integer $i"; };

#|(myfunc accepts a string.)
multi my-multi-func (Str $s) { say "You provided a string $s"; };

#`[
And this is how a multi would work.
You can still access every documentation. Somehos
]

say "my-multi-func is a {&my-multi-func.perl} and does {&my-multi-func.WHY}";

## multi with position variable

multi sub Screen_get_valid_string($prompt, $accept_empty_string where $_ == True|False,  $max_length = 999) { return "abc" }
multi sub Screen_get_valid_string($prompt, $max_length = 999)                       { return "def" }

my $return = Screen_get_valid_string("enter value for string => ", True);

say 'return  is ', $return;

## overloading

# Operator overloading in Perl 6 will be done by multi-dispatch routines
# (In fact, all of the built-in operators are invoked by a multi-dispatch.

# Dispatch By Arity
# Arity = number of arguments that a routine takes
# Could do the previous example as:

multi sub greet($name) {
    say "Ahoj, $name!";
}
multi sub greet($name, $greeting) {
    say "$greeting, $name!";
}
greet('Anna'); # Ahoj Anna 
greet('Лена', 'Привет '); # Привет, Лена"

# Type-Based Dispatch
# We can write types in a signature
# They are used to help decide which candidate to call

multi sub double(Int $x) {
    return 2 * $x;
}
multi sub double(Str $x) {
    return "$x $x";
}
say double(21);      # 42
say double("he");   # he he

## multi with default

multi foo(Int $x) { 1 }
multi foo(Int $x) is default { 2 }
say foo(1); # 2

## multi with callwith

class Wide             {}
class Middle is Wide   {}
class Narrow is Middle {}
multi foo(Narrow $v) { say 'Narrow', $v; 'from Narrow' }
multi foo(Middle $v) {
    say 'Middle', $v;

    my $result = callwith Narrow;
    say "We're back! The return value is $result";

    'from Middle'
}
multi foo(Wide $v) { say 'Wide ', $v; 'from Wide' }

foo Middle;

## multi with callsame

#class Wide             {}
#class Middle is Wide   {}
#class Narrow is Middle {}

#multi foo(Narrow $v) { say 'Narrow', $v, 'from Narrow' }
multi foo(Middle $v) {
    say 'Middle', $v;

    my $result = callsame;
    say "We're back! The Return value is $result";

    'from Middle'
}

multi foo(Wide $v) { say 'Wide ', $v; 'from Wide' }

foo Middle;