[Parrot-users] A length() runtime written in NQP

Jonathan "Duke" Leto jonathan at leto.net
Wed Jul 6 15:38:07 UTC 2011


Howdy,

On Wed, Jul 6, 2011 at 8:22 AM, Jay Emerson <jayemerson at gmail.com> wrote:
> I'm trying to write a function which would behave something like (pseudo-code):
>
> a = 0
> b = (1, 2, 3)
> length(a)       # Returns 1
> length(b)       # Returns 3

Just to clarify, you want a single number or string passed in to
return a length of 1, but aggregate types should return the number of
elements in them?

Do you still want length(555) = 1 and length("555") = 1, or should they be 3 ?

>
> Now, in a squaak-like language, I think a is a scalar and b is a pmc.
> I tried the following:
>
> sub length(*@args) {
>    my @arg := @args[0];
>    my $len := @arg;
>    pir::return($len);
> }

One gotcha about NQP is that the sigils (@ and $) are lies, they are just
namespaces that you can use, and NQP doesn't care if you store an array in $foo
or a scalar in @bar.

Also, is there a reason you are using pir::return() ? I think:

    return $len;

should work.

Duke

-- 
Jonathan "Duke" Leto <jonathan at leto.net>
209.691.DUKE // http://leto.net
NOTE: Personal email is only checked twice a day at 10am/2pm PST,
please call/text for time-sensitive matters.


More information about the Parrot-users mailing list