[Parrot-users] Language philosophy
Will Coleda
will at coleda.com
Wed Jul 6 17:01:20 UTC 2011
On Wed, Jul 6, 2011 at 12:20 PM, Jay Emerson <jayemerson at gmail.com> wrote:
> Is it necessary to have a scalar atomic object (is this called a
> literal?), or could this be an array (I'd call it a vector) of length
> 1? Relating to my other questions,
>
> sub print(*@args) {
> my $arg := @args[0];
> pir::say(pir::join(' ', $arg));
> pir::return();
> }
>
> works if the first argument is an array but not if it is a scalar
> integer or float. So if a scalar integer or float were an array of
> length 1 then there wouldn't be those two cases to deal with. In the
> function above, how would I know if $arg contained a scalar (literal)
> or an array?
While you could make a parrot PMC that acted this way, you can also
rely on multi-dispatch or type interrogation to get the behavior you
want:
$ cat foo.nqp
sub print(*@args) {
my $arg := @args[0];
if pir::does($arg, "array") {
say(pir::join(' ', $arg));
} else {
say($arg);
}
}
print(3);
print(1,2,3);
print( [1,2,3] );
$ parrot-nqp foo.nqp
3
1
1 2 3
"does" in parrot is something role-like (but not actually a Role) that
each PMC can report pretty much freetext in, but all the core PMCs
report as expected for array, hash, and a few others.
(multisub example to follow shortly)
> I ask because the + at anarray trick to find the length of an array
> worked, but produced a float scalar, leading me to the discovery that
> this behaves differently from an array of length 1. Not a huge
> surprise, but leads me to think about how to best organize my 'life'
> ...
>
> Jay
>
> --
> John W. Emerson (Jay)
> Associate Professor of Statistics
> Department of Statistics
> Yale University
> http://www.stat.yale.edu/~jay
> _______________________________________________
> Parrot-users mailing list
> Parrot-users at lists.parrot.org
> http://lists.parrot.org/mailman/listinfo/parrot-users
>
--
Will "Coke" Coleda
More information about the Parrot-users
mailing list