Skip to content

Instantly share code, notes, and snippets.

@jonathanstowe
Created December 4, 2021 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanstowe/3d7e85cfc0518f0f9941a0c7c861fec6 to your computer and use it in GitHub Desktop.
Save jonathanstowe/3d7e85cfc0518f0f9941a0c7c861fec6 to your computer and use it in GitHub Desktop.
Returning an Array from a raku subroutine
# Or if you want to be check each element then you can
# create a subset that does so
subset ArrayOfInt of Array where { .all ~~ Int };
sub d(Int $a, Int $b --> ArrayOfInt) {
[$a, $b];
}
say d(1, 2);
# Because the type of the Array needs to match one may
# opt for looser typing
sub d(Int $a, Int $b --> Array) {
[$a, $b];
}
say d(1, 2);
# In order to have the typed array the return value must
# be the same type - the parameter to the Array is part of the type
sub d(Int $a, Int $b --> Array[Int]) {
Array[Int].new($a, $b);
}
say d(1, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment