Re: Operator overloading with returning lists?
by JavaFan (Canon) on Nov 27, 2008 at 12:00 UTC
|
I want to overload an operator (ideally '~~') but want the result to be a list. It seems like that's not possible, the list is only handled like in scalar context.
Indeed. There were some messages on p5p about this quite recently. I think the outcome was "No, that's not possible. The implementor (Ilya) of overloading deemed it not necessary to be able to ask the context - that is, the operands are always in scalar context".
Note that when overloading was implemented, '~~' wasn't an operator, and only a few overloadable operators would behave differently in list context anyway. (<> being the obvious on. x behaves different in list context as well, but only if its LHS has parens, making it an odd one).
| [reply] [Watch: Dir/Any] |
|
>perl -le"$x = 'x' x 5; print $x"
xxxxx
>perl -le"$x = ( 'x' x 5 )[0]; print $x"
xxxxx
>perl -le"$x = ( ('x') x 5 )[0]; print $x"
x
The only time context matters, x behaves normally.
>perl -le"$x = ('x') x 5; print $x"
xxxxx
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
x behaves different in list context as well
Not a very good example. It's more the LHS operand than the context which determines what is returned.
That's not all I said. I immediately followed it (not even stopping to start a new sentence) with
but only if its LHS has parens, making it an odd one
Both context and the LHS matter, just as I said:
my $w = 'x' x 5; say "$w"; # Scalar context, no parens.
my $x = ('x') x 5; say "$x"; # Scalar context, parens.
my @y = 'x' x 5; say "@y"; # List context, no parens.
my @z = ('x') x 5; say "@z"; # List context, parens.
__END__
xxxxx
xxxxx
xxxxx
x x x x x
| [reply] [Watch: Dir/Any] [d/l] |
|
|
|
|
|
|
Re: Operator overloading with returning lists?
by ysth (Canon) on Nov 27, 2008 at 12:04 UTC
|
Overloaded operators generally cannot return lists. But they can return objects that contain lists, as in the case of Quantum::Superpositions.
| [reply] [Watch: Dir/Any] |
Re: Operator overloading with returning lists?
by LanX (Saint) on Nov 27, 2008 at 12:51 UTC
|
Contextual::Return should help you! AFAIS the principle of returning an object which transforms later (to a list in your case) seems to be the same like in the already mentioned Quantum::Superpositions
UPDATES:
+ fixed links.
+ after a quick look at Quantum::Superpositions I think Contextual::Return fits much better into your needs.
+ PS: If this works, I'd be glad to get feedback about it 8 ) | [reply] [Watch: Dir/Any] |
|
Thanks for all your answers. Contextual::Return was a very nice idea. Unfortunately it did not work for the operator. Although C::Return allows more precise detection than the normal wantarray it's still the wrong context in which the expression is evaluated, so I could not get to to use in anything else than SCALAR.
I heard that '~~' should slightly change its semantics in 5.10.1, maybe it would be possible to even more enhance it with context awareness, e.g. in order to harmonize it with the Perl6 operator (do those allow context detection?). I should now ask in p5p ...
Thanks again.
| [reply] [Watch: Dir/Any] |
|
I don't understand exactly what you your aim is. Could you give us more use cases?
The LHS is a scalar or is it an array/list???
| [reply] [Watch: Dir/Any] |
|
|
|
Re: Operator overloading with returning lists?
by diotalevi (Canon) on Nov 30, 2008 at 08:13 UTC
|
If your overloaded function was written in XS, I could imagine it might be possible to ignore the context and foricibly return multiple things. Just push everything onto the stack even though you're always in scalar context.
| [reply] [Watch: Dir/Any] |
|
I don't think that will work if the caller expects just one return value.
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|