develooper Front page | perl.perl5.porters | Postings from December 2017

[perl #132490] context consistency for overloaded operators

From:
KES via RT
Date:
December 11, 2017 12:38
Subject:
[perl #132490] context consistency for overloaded operators
Message ID:
rt-4.0.24-3467-1512995903-821.132490-15-0@perl.org
>Should context be passed through when operators invoke overloaded methods

Because operators implemented as subroutines this subroutines should know the context they are called.

>The purpose of overloading isn't to turn operator invocations into arbitrary method calls;
Why not? Operator call is just subroutine call. This is simple and did not force user to know something special.

>allowing an operator that normally yields a scalar result to return a list would complicate all sorts of op processing
I think here we should just distinguish unary and list operators/subroutines.
Unary should always produce the scalar:
%h = (
    x => !$obj,               # call in list context, but we return scalar 
    y => scalar $obj->method, # call in list context, but we return scalar
)
I will agree that list result should be prohibited for calls parsed as unary operators: sub myrand (;$)/sub xxx($)

>The purpose of overloading is to allow an object to provide behaviour that fits into the general concept of the operator. Propagating void context doesn't advance that aim
I do not see any reason to prohibit context check.
For example we can **skip hard work** when somebody call operator in void context (Not just emit warning)

>The general concept of these operators includes that they perform the same operation regardless of context
These always lead to confusion when 

    # https://perldoc.perl.org/perlop.html#Logical-Defined-Or
    @a = @b || @c;            # This doesn't do the right thing

Also without context propagation we will not be able to implement sets multiplication:

    @multi = @z * @y;
    @multi = (1,2,3)*(3,4);
   print @multi # [1,3],[1,4],[2,3],[2,4],[3,3],[3,4]



---
via perlbug:  queue: perl5 status: new
https://rt.perl.org/Ticket/Display.html?id=132490



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