Re: Can overloaded '+' return an array? If so, how?
- From: xhoster@xxxxxxxxx
- Date: 30 Aug 2006 19:16:17 GMT
"PeterSShenkin" <shenkin@xxxxxxxxx> wrote:
The following sample code prints:
combine: result= [1 2 3 4]
main: ar = [4]
I expect it to print:
combine: result= [1 2 3 4]
main: ar = [1 2 3 4]
The issue is that "combine", which is the function that '+' maps to,
returns an array. When, in the main program, I say:
my @ar = $ca1 + ca2
I expect the "combine" function to be called in list context. But
evidently it is called in scalar context, because my @ar variable
receives the cardinality of the "result" array, rather than a copy of
the array.
Looking at Ch 13 of the Camel book, I don't see any restriction on
return values of overloaded operators; but maybe I'm just missing it.
If there's a way to do it, but I'm doing it wrong, I'd like to know
that. Either way, please advise.
Your understanding seems to be correct (all overloaded operators are
called in scalar context), and you seem to be correct that this is not
well documented. The closest thing to it seems to be, under overload's
section on <> operator:
BUGS Even in list context, the iterator is currently called
only once and with scalar context.
(This fact seems to be true for all operators, but apparently is only
considered to be a bug for the <> operator.)
(If, instead of what I do in the example, I return a reference to the
array, and alter my main routine correspondingly, then, of course,
everything works.)
Yep. And, in fact, I would usually expect an overloaded addition
operator to turn something of the same class as (at least one of) it's
operands, so I suggest that not only should it return a ref, but a ref
that is blessed into ClassAct.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
.
- Follow-Ups:
- Re: Can overloaded '+' return an array? If so, how?
- From: PeterSShenkin
- Re: Can overloaded '+' return an array? If so, how?
- References:
- Can overloaded '+' return an array? If so, how?
- From: PeterSShenkin
- Can overloaded '+' return an array? If so, how?
- Prev by Date: Re: imagemagick very slow - is there anything better?
- Next by Date: Re: Can overloaded '+' return an array? If so, how?
- Previous by thread: Re: Can overloaded '+' return an array? If so, how?
- Next by thread: Re: Can overloaded '+' return an array? If so, how?
- Index(es):
Relevant Pages
|