Re: [RFC] User Defined Operator Overloads (v0.6)

From: Date: Sun, 12 Dec 2021 18:58:08 +0000
Subject: Re: [RFC] User Defined Operator Overloads (v0.6)
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-116629@lists.php.net to get a copy of this message
On 12/12/2021 18:29, Rowan Tommins wrote:
Perhaps we could use an Attribute to bind the operator to the method, which would also reduce the impact on tools that need to parse class definitions: class Collection{ #[Operator('+')]
    public function  union(Collection$other,  OperandPosition$operandPos)  {}
} An interesting extension would be to have an optional argument to the Attribute which binds separate methods for each direction of arguments, rather than exposing it as a parameter: class Number{ #[Operator('/', OperandPosition::LeftSide)]
    public function  divideBy(Number $divisor)  {}
#[Operator('/', OperandPosition::RightSide)]
    publicfunction  fractionOf(Number $dividend)  {}
} 
Sorry about the whitespace mess in the above examples; this may or may not show better: class Collection{
    #[Operator('+')]
    public function union(Collection $other,  OperandPosition $operandPos)  {}
} class Number{
    #[Operator('/', OperandPosition::LeftSide)]
    public function divideBy(Number $divisor)  {}
    #[Operator('/', OperandPosition::RightSide)]
    public function fractionOf(Number $dividend)  {}
} Regards, -- Rowan Tommins [IMSoP]

Thread (45 messages)

« previous php.internals (#116629) next »