Quick actions

cmd+k|ctrl+k

Navigation

Languages

Prototype subroutines

Snippet info

Language

Raku

Visibility

public

Author

chacewells

Created

2017-11-08T21:41:12Z

Updated

2017-11-08T21:41:55Z

use v6;

# wat prototype. the {*} marks where child subroutines fill in the blank
# $*yes is a dynamic variable (i.e. it doesn't have to be declared in the child subroutines to be used)
proto sub wat($first, $second, |) {
	my $*yes = "$first $second";
	{*}
}

# dispatch here when 3rd argument Int
multi sub wat($a, $b, Int $third) {
	join ' ', $*yes xx $third
}

# dispatch here when 3rd argument Str
multi sub wat($a, $b, Str $third) {
	"$*yes $third"
}

my @int-dispatch-args = 'foo', 'bar', 4; 			# triggers Int dispatch
my @str-dispatch-args = 'finger', 'food', 'fad'; 	# triggers Str dispatch

say "wat(Int dispatch with prototype): ", wat |@int-dispatch-args;
say "wat(Str dispatch with prototype): ", wat |@str-dispatch-args;

# helper method style. this will be used by the Int and Str woot dispatches
multi sub woot($first, $second) {
	"$first $second"
}

# Int dispatch using helper subroutine
multi sub woot($first, $second, Int $third) {
	join ' ', woot($first, $second) xx $third
}

# Str dispatch using helper subroutine
multi sub woot($first, $second, Str $third) {
	"&woot($first, $second) $third"
}

say 'woot(Int dispatch no prototype): ', woot |@int-dispatch-args;
say 'woot(Str dispatch no prototype): ', woot |@str-dispatch-args;
INFO