Re: Revisiting Userland Operator Overloads
| From: | Larry Garfield | Date: | Sat, 07 Aug 2021 14:28:54 +0000 |
| Subject: | Re: Revisiting Userland Operator Overloads | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-115652@lists.php.net to get a copy of this message | ||
On Fri, Aug 6, 2021, at 3:18 PM, Scott Arciszewski wrote:
> My recommendation is to have a base interface for every operator that can
> be overloaded, so that these can be composed together.
>
> AddOverloadInterface { __overloadAdd(): self; }
> SubOverloadInterface { __overloadSub(): self; }
> MulOverloadInterface { __overloadMul(): self; }
> DivOverloadInterface { __overloadDiv(): self; }
> ModOverloadInterface { __overloadMod(): self; }
> LeftShiftOverloadInterface { __overloadLeftShift(): self; }
> RightShiftOverloadInterface { __overloadRightShift(): self; }
> XorOverloadInterface { __overloadXor(): self; }
> etc.
>
> Then if you were implementing matrix math, you could do this:
>
> MatrixMath implements AddOverloadInterface, SubOverloadInterface,
> MulOverloadInterface {
>
> }
That would be roughly how Stringable/__toString works now. I'd be OK with that, if it's
going to be a language trend.
> This prevents you from having to define methods for operations you don't
> support.
>
> It's probably worth exploring whether common combinations are worth
> defining for convenience.
Now that intersection types are a thing, I don't think combo interfaces are worth much here.
If anything, it would be just more impetus for type aliases to reduce typing. (Human typing, I
mean, not code typing.)
--Larry Garfield