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

[perl #132490] context consistency for overloaded operators

KES via RT
December 11, 2017 12:38
[perl #132490] context consistency for overloaded operators
Message ID:
>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 

    @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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About