On Fri, Jun 28, 2024 at 10:47 AM Rob Landers <rob@bottled.codes> wrote:

> Hello internals,
>
> I'd like to introduce a new RFC:
> https://wiki.php.net/rfc/operator_overrides_lite which extends the GMP
> extension to support a limited set of operator overriding to developers.
> It's designed to be limited and relatively simple, while still being quite
> powerful. It would only be available if you have the GMP extension
> installed and enabled, but would allow for creating powerful unit libraries
> (such as representing money, duration, etc).
>
> I'm very interested in your feedback!
>
> This was initiated from a discussion in another thread:
> https://externals.io/message/123872
>
> Thanks for your time,
>
> Rob Landers
>

You probably have not actually looked at implementing this yet, so let me
give you some advice:

1. You probably are not yet aware that operands get reordered, or their
ordering is not guaranteed, when a comparison occurs. This is because there
is no op code (currently) for Greater Than or Greater Than Or Equal.
Instead the comparison is reordered so that the operands are swapped and a
Less Than or a Less Than Or Equal is performed. This is perfectly
acceptable until you need to determine whether the left or right operand's
function needs to be called, such as with objects.
2. The fact that the signature uses `mixed` for the typing removes a lot of
the safety features the previous RFC had, and makes a lot of the concerns
that stopped that RFC worse. In PHP currently, an object used with any
non-comparison operand results in a TypeError. In the previous proposal,
this behavior was preserved *even for objects which implement overloads*
unless they specifically listed the type as accepted in the overload
definition. What this did is ensure that anything OTHER than a TypeError
guaranteed the developer that they were dealing with an operator overload
that they could go inspect.
3. The private/protected distinction is fairly meaningless for the
functions that implement overloads, because the privacy of the function is
ignored completely when it is executed to evaluate an operator.
4. The `static` distinction is also fairly meaningless, as in PHP there is
no situation possible where an operator overload can occur WITHOUT it
operating on objects themselves.
5. Your voting choices actually constitute something that is not allowed
for RFCs. A 'yes' vote allows operator overloads for GMP, a 'no' vote
changes GMP to `final`. There is no option here to leave PHP as it
currently is, which is what a 'no' vote should mean.
6. The `comparable` function you propose doesn't actually have an operator
it corresponds to. There is no operator in PHP for "is the left value
comparable with the right value". There are operators for comparisons
themselves, which I assume you meant, but a bool is insufficient as a
return type for that.

There's probably more, but this is a start for you to make some
improvements and changes. However, I had to warn you before you put in too
much effort that I am fairly certain this RFC has very nearly zero chance
of passing.

Jordan

Reply via email to