develooper Front page | perl.perl6.internals | Postings from September 2002

Re: [netlabs #801] [PATCH] PerlArray in scalar context

Thread Previous | Thread Next
From:
Peter Haworth
Date:
September 4, 2002 08:22
Subject:
Re: [netlabs #801] [PATCH] PerlArray in scalar context
Message ID:
PGM.20020904152122.25085.2452@edison.ioppublishing.com
[OSCON has left me way behind in reading my mail, so apologies for the late reply]

On Wed, 14 Aug 2002 16:45:09 -0500 (CDT), David M. Lloyd wrote:
> Here's how I'd expect these expressions to be executed internally, in
> gross pseudocode, ignoring for the moment the multimethod vaporware:
> 
> $r = $a + $b;             # $a->add($a, $b, $r)
> $r = @a + $b;             # $t = @a->get_pmc(@a); $t->add($t, $b, $r)
> $r = $a + @b;             # $t = @b->get_pmc(@b); $t->add($a, $t, $r)
> $r = @a + @b;             # $t = @a->get_pmc(@a); $u = @b->get_pmc(@b);
>                           # $t->add($t, $u, $r);

get_pmc() seems a bit vague to me. Shouldn't it be get_scalar(), or even
get_number()?

I've reordered the next bit to make my point more obvious.

> @r = @a ^+ @b;            # @a->add(@a, @b, @r), easy

As Sean says in his reply, making the array vtable perform hyper operations
probably means having some code duplication. However, you can get around this
by using a helper function which will do the hyper bits, iterating through a
pair of array PMCs, applying a supplied function (with a supplied default for
the case where one array is exhausted). This means that array vtable functions
would mostly just consist of calls to the helper function. However, see below.
  
> @r = @a ^+ $b;            # Does this distribute? If so,
>                           # @a->add(@a, $b, @r) else see above

It does distribute. However, you can't do the detection of this case entirely
in the vtable, because then C< @a ^+ $b > and C< @a ^+ @$b > would end up
being indistinguishable (though you probably wouldn't do the first very often
with an arrayref).

Having the iteration done in the bytecode could make this simpler, although
at the expense of needing more bytecode. You can probably have bytecode ops
specifically for hyping operators though, so it might not be too bad. Also,
it means we can use the same mechanism for hyping non-vtable methods.

> @r = $a ^+ @b;            # See above

Yes, but I'm not convinced that scalar-type PMCs should be responsible for
detecting array-type PMCs passed as the second operand. Having the bytecode
do the iteration in this case means that the appropriate vtables get used,
and the scalar-type vtable routines don't get complicated with hyperoperator
decisions, which will probably be a minority of cases for the first operand
being a scalar.

> @r = $a ^+ $b;            # Something that makes one-elment arrays and
>                           # uses add method on array object?  Or perhaps
>                           # error

Bytecode iteration means that this just ends up calling the normal scalar op,
and stuffing the result into an array.


Looks like I'm in favour of bytecode iteration for hyperoperators after all.

-- 
	Peter Haworth	pmh@edison.ioppublishing.com
I is for indent, which rarely amuses, and
J is for join, which nobody uses.
K is for kill, which makes you the boss, while
L is for lex, which is missing from DOS.
		-- The ABC's of Unix

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About