Created
December 4, 2021 10:49
-
-
Save jonathanstowe/3d7e85cfc0518f0f9941a0c7c861fec6 to your computer and use it in GitHub Desktop.
Returning an Array from a raku subroutine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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