[Parrot-users] Language philosophy

Will Coleda will at coleda.com
Wed Jul 6 17:14:44 UTC 2011


On Wed, Jul 6, 2011 at 1:01 PM, Will Coleda <will at coleda.com> wrote:
> 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)

$ cat bar.nqp
our multi sub myprint(ResizablePMCArray @args) {
   say(pir::join(' ', @args));
}

our multi sub myprint($arg) {
   say($arg);
}

myprint(3);
# myprint(1,2,3); # will error out because no signature matches.
myprint( [1,2,3] );
$ parrot-nqp bar.nqp
3
1 2 3

The multi dispatch relies on the type of the argument, so in this
case, you need the type of the array - the type we're generating with
[1,2,3] is a resizable PMC array, which inherits from a
fixedpmcarray... which doesn't inherit from Array. (This is probably
why we have a "does" in the first place, because the hierarchy in PMCs
is more about internals than APIs).

Note that we have renamed the function, so that it doesn't conflict
with nqp's builtin "print". We've also added an "our" to change the
scope of the subs (otherwise we end up a parrot error complaining
about redefining the lexical sub.)

Also, given the 2 multis I've defined, the middle case, where we'd
originally just pull out the first arg, is no longer valid - there's
nothing for that to dispatch to, since we're now relying on the
signatures of the subs and the types of the args to find the right
code path.

Hope this helps.

>> 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
>



-- 
Will "Coke" Coleda


More information about the Parrot-users mailing list