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

- Thread: https://externals.io/message/116611
- Original archive: https://news-web.php.net/php.internals/116611
- Started by Jordan LeDoux on 2021-12-09T20:11:51+00:00
- Messages: 45
- RFC: User-Defined Operator Overloads (Declined) — https://externals.io/rfc/user_defined_operator_overloads.md
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/116611.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #116611 — Jordan LeDoux — 2021-12-09T20:11:51+00:00

URL: https://externals.io/message/116611#116611

Hello internals,

I last brought this RFC up for discussion in August, and there was
certainly interesting discussion. Since then there have been many
improvements, and I'd like to re-open discussion on this RFC. I mentioned
in the first email to the list that I was planning on taking a while before
approaching a vote, however the RFC is much closer to vote-ready now, and
I'd like to open discussion with that in mind.

RFC Link: https://wiki.php.net/rfc/user_defined_operator_overloads

There is a patch for this RFC, however the latest commits are not playable.
It will build, but with various problems which are being worked on related
to enums. The last playable commit can be found by checking out this commit:

https://github.com/JordanRL/php-src/commit/e044f53830a9ded19f7c16a9542521601ac3f331

This commit however does not have the enum for operator position described
in the RFC. It uses a bool instead with true being the left side, and false
being the right side.

Implementation details still left:
- There are issues related to opcache/JIT still, so if you want to play
around with the playable commit disable both.
- Reflection has not been updated, but the proposed updates necessary are
described in the RFC.

It is a long RFC, but operator overloads are a complicated topic if done
correctly. Please review the FAQ section before asking a question, as it
covers many of the main objections or inquiries to the feature. I'd be
happy to expand on any of the answers there if prompted however.

Jordan

---

## #116620 — Dan Ackroyd — 2021-12-11T15:15:34+00:00

URL: https://externals.io/message/116611#116620 · In reply to: #116611

Hi Jordan,


>
> Hello internals,
>
> I last brought this RFC up for discussion in August, and there was
> certainly interesting discussion. Since then there have been many
> improvements, and I'd like to re-open discussion on this RFC.

In general I'm in favour of this RFC; a few months ago I was
programming something and operator overloads would have been a good
solution, but then I remembered I was using PHP, and they haven't been
made possible yet.

However.....I think the new 'operator' keyword is probably not the way
to go. Although it's cute, it has some significant downsides.

There are quite a few downstream costs for making a new type of
methods in classes. All projects that analyze code (rector,
codesniffer, PHPStorm, PhpStan/Psalm, PHPUnit's code coverage
annotations etc) would have to add a non-trivial amount of code to not
bork when reading the new syntax. Requiring more code to be added and
maintained in PHP's builtin Reflection extension is also a cost.
That's quite a bit of work for a feature that has relatively rare
use-cases.

I just don't agree/understand with some of the reasoning in the RFC of
why using symbols is preferable.

"In such a situation, using magic methods would not be desired, as any
combination of symbols may be used for the new infix. The restrictions
on function names, such as needing to reserve the & to mark a function
as being by-reference, would place limitations on such future scope."

I don't get this. The magic methods in previous drafts of the RFC
don't have a problem with & as the methods are named with 'two
underscores' + name e.g. __bitwiseAnd. That does't appear to cause a
problem with an ampersand?

"By representing the implementations by the symbols themselves, this
RFC avoids forcing implementations to be mislabeled with words or
names which do not match the semantic meaning of that symbol in the
program context.

The name of the function (e.g. __add) always refers to the symbol used
where it is used, not what it is doing.

If the code is `$a + $b` then that is an addition operator, when the
code is read. If I was reading the code, and I saw that either  $a or
$b were objects, I would know to go looking for an __add magic method.

" '// This function unions, it does not add'"

Then that is probably an example of an inappropriate use of operator
overloads, and so shouldn't be used as a justification for a syntax
choice.

# "Non-Callable - Operand implementations cannot be called on an
instance of an object the way normal methods can."

I think this is just wrong, and makes the RFC unacceptable to me.

Although most of the code I write is code that just performs
operations as I see fit, some of the time the operations need to be
driven by user data. Even something simple like a
calculator-as-a-service would need to call the operations dynamically
from user provided data.

I also have an aesthetic preference when writing tests to be explicit
as possible, rather than concise as possible e.g.

$foo->__add(5, OperandPosition::LeftSide);
$foo->__add(5, OperandPosition::RightSide);

instead of:

$foo + 5;
5 + $foo

As I find that easier to reason about.

cheers
Dan
Ack

/congratulations on stunning the audience into silence otherwise though.

---

## #116621 — Larry Garfield — 2021-12-11T17:09:03+00:00

URL: https://externals.io/message/116611#116621 · In reply to: #116620

> Hi Jordan,
>
> 
> > […]
>
> In general I'm in favour of this RFC; a few months ago I was
> programming something and operator overloads would have been a good
> solution, but then I remembered I was using PHP, and they haven't been
> made possible yet.

I too far prefer this RFC to its predecessors, and hope it passes in some form.

> However.....I think the new 'operator' keyword is probably not the way
> to go. Although it's cute, it has some significant downsides.
>
> There are quite a few downstream costs for making a new type of
> methods in classes. All projects that analyze code (rector,
> codesniffer, PHPStorm, PhpStan/Psalm, PHPUnit's code coverage
> annotations etc) would have to add a non-trivial amount of code to not
> bork when reading the new syntax. Requiring more code to be added and
> maintained in PHP's builtin Reflection extension is also a cost.
> That's quite a bit of work for a feature that has relatively rare
> use-cases.
>
> I just don't agree/understand with some of the reasoning in the RFC of
> why using symbols is preferable.
>
> "In such a situation, using magic methods would not be desired, as any
> combination of symbols may be used for the new infix. The restrictions
> on function names, such as needing to reserve the & to mark a function
> as being by-reference, would place limitations on such future scope."
>
> I don't get this. The magic methods in previous drafts of the RFC
> don't have a problem with & as the methods are named with 'two
> underscores' + name e.g. __bitwiseAnd. That does't appear to cause a
> problem with an ampersand?
>
> "By representing the implementations by the symbols themselves, this
> RFC avoids forcing implementations to be mislabeled with words or
> names which do not match the semantic meaning of that symbol in the
> program context.
>
> The name of the function (e.g. __add) always refers to the symbol used
> where it is used, not what it is doing.
>
> If the code is `$a + $b` then that is an addition operator, when the
> code is read. If I was reading the code, and I saw that either  $a or
> $b were objects, I would know to go looking for an __add magic method.
>
> " '// This function unions, it does not add'"
>
> Then that is probably an example of an inappropriate use of operator
> overloads, and so shouldn't be used as a justification for a syntax
> choice.

I believe the intent is for things like dot product.

int * int

This is known as "multiplication", or we could abbreviate it __mul if we felt like it.

vector * vector

This is known as a "Dot product", or "scalar product", and is not really the same as multiplication.  It uses effectively the same operator sigil, though.

There's also cross-product, which nominally uses `x` as a symbol in mathematics.  Which is... often also translated to * in code, but is a very different operation and is not multiplication as we know it, nor is it the same as dot product.

So __mul() would be an incorrect name for either one.  Using symbols, however, would (with some future extension to make it extensible) allow for:

operator *(Vector $v, $left) { ... }

operator x(Vector $v, $left) { ... }

which (for someone who knows vector math) is a lot more self-explanatory than "which one is being mistranslated as multiply?"

At least, that's my understanding of the argument for it.  The point about making meta programming more difficult is certainly valid, though.  Personally I think I could go either way on this one, as I see valid arguments either direction.

> # "Non-Callable - Operand implementations cannot be called on an
> instance of an object the way normal methods can."
>
> I think this is just wrong, and makes the RFC unacceptable to me.
>
> Although most of the code I write is code that just performs
> operations as I see fit, some of the time the operations need to be
> driven by user data. Even something simple like a
> calculator-as-a-service would need to call the operations dynamically
> from user provided data.

I largely agree here.  I don't know if it's because of the `operator` choice or not, but being able to call an operator dynamically is important in many use cases.  It doesn't have to be a pristine syntax, but some way to do that dynamically (without having a big match statement everywhere you need to) would be very welcome.

Another question: Can an interface or abstract class require an operator to be implemented?  That's not currently discussed at all.  (I would expect the answer to be Yes.)

--Larry Garfield

---

## #116622 — Jordan LeDoux — 2021-12-11T19:13:41+00:00

URL: https://externals.io/message/116611#116622 · In reply to: #116620

>
> There are quite a few downstream costs for making a new type of
> methods in classes. All projects that analyze code (rector,
> codesniffer, PHPStorm, PhpStan/Psalm, PHPUnit's code coverage
> annotations etc) would have to add a non-trivial amount of code to not
> bork when reading the new syntax. Requiring more code to be added and
> maintained in PHP's builtin Reflection extension is also a cost.
> That's quite a bit of work for a feature that has relatively rare
> use-cases.
>

While I'm not suggesting that this isn't an important consideration for
voters with this RFC, (I think it should be weighed for sure), I *think*
that all of the things you mentioned will need similar updates to work
correctly with this RFC even if it was done with plain old magic methods
instead. Again, I'm not saying it's not important, but isn't this true of
many RFCs that add new syntax, such as enums or even things like array
unpacking?

As for Reflection, I hadn't gotten to that yet, but I actually pushed the
commit for it with full test coverage of the changes last night. It wasn't
actually that big of a change, and the latest commit on the PR can be
checked out and played if you want to test it. With support for the enum
casing as well. Operators get an additional function flag ZEND_ACC_OPERATOR
that made it fairly simple to implement the Reflection changes with minimal
additional code. The new methods I mentioned are actually smaller that the
implementations for the existing ones. As far as maintainability goes,
removing this aspect doesn't make this RFC more maintainable in core in my
opinion. It becomes harder to maintain it I think, as it requires more
special casing in other places that is more obtuse and obscure.


> I don't get this. The magic methods in previous drafts of the RFC
> don't have a problem with & as the methods are named with 'two
> underscores' + name e.g. __bitwiseAnd. That does't appear to cause a
> problem with an ampersand?
>

This was referring to continuing to use symbol names, but without a new
keyword.

The name of the function (e.g. __add) always refers to the symbol used
> where it is used, not what it is doing.
>
> If the code is `$a + $b` then that is an addition operator, when the
> code is read. If I was reading the code, and I saw that either  $a or
> $b were objects, I would know to go looking for an __add magic method.
>

True, and I don't think it would be *ambiguous* this way. However, method
names for other methods tend to describe what the function does.


>
> " '// This function unions, it does not add'"
>
> Then that is probably an example of an inappropriate use of operator
> overloads, and so shouldn't be used as a justification for a syntax
> choice.
>

Larry's example with dot-product and cross-product is a better example, but
one that is less accessible to those who don't know vector math/linear
algebra. I used this example because this is exactly how PHP treats + for
arrays, so I would think most Collections would implement + as the union
operator in order to remain consistent with internals.


> I think this is just wrong, and makes the RFC unacceptable to me.
>
> Although most of the code I write is code that just performs
> operations as I see fit, some of the time the operations need to be
> driven by user data. Even something simple like a
> calculator-as-a-service would need to call the operations dynamically
> from user provided data.
>

I'm confused why this wouldn't still be possible? First, you can still get
the closure for the operator implementation from Reflection if you really,
really need it. But second, with user data couldn't you use a setter to
change the object state prior to the op, use a method specifically for
calling as a method, or just combine them with the operator?

`$obj->setValue($userData);`

`$result = $obj + $userData;`


>
> I also have an aesthetic preference when writing tests to be explicit
> as possible, rather than concise as possible e.g.
>
> $foo->__add(5, OperandPosition::LeftSide);
> $foo->__add(5, OperandPosition::RightSide);
>
> instead of:
>
> $foo + 5;
> 5 + $foo
>
> As I find that easier to reason about.
>
>
This I can understand. Again, I don't think you're wrong here, I think this
is a matter of opinion and taste. I can understand your position, but I
think the long-term maintainability and support the keyword offers is worth
the short term pain.

Larry:

> I largely agree here.  I don't know if it's because of the `operator`
choice or not, but being able to call an operator dynamically is important
in many use cases.  It doesn't have to be a pristine syntax, but some way
to do that dynamically (without having a big match statement everywhere you
need to) would be very welcome.

Overall, the point in making them non-callable was to force PHP developers
to stop thinking about these as methods. They are modifications to engine
behavior that are given directly to PHP devs. Using them as methods would
*usually* indicate incorrect usage. If that's what you need, then probably
you need a method, not an operator overload. If you need *both*, then what
I would suggest is implementing the logic as a normal method, and then
calling that method inside of the operator overload.

> Another question: Can an interface or abstract class require an operator
to be implemented?  That's not currently discussed at all.  (I would expect
the answer to be Yes.)

Yes. Both abstract classes and interfaces can require implementations of
the operator keyword as they would with a method.

Jordan

---

## #116623 — James Titcumb — 2021-12-12T08:55:10+00:00

URL: https://externals.io/message/116611#116623 · In reply to: #116611

>
> RFC Link: https://wiki.php.net/rfc/user_defined_operator_overloads


I'm not strongly opinionated on either approach (magic __add vs operator +)
although I have a very slight preference to your propose operator + syntax,
however despite very occasionally thinking that this would be a useful
feature, I always start to think about all the awful ways this could be
used, making debugging and reasoning about code harder, since unexpected
magic behaviour can (and probably will be) introduced.

The proposal is technically reasonable, pending consideration of reflection
and so on, but I just think the desire to use this in ways it shouldn't be
used will be too great, and we'll end up with horrendous complexity.
Perhaps I'm too cynical.

The only mitigation for unnecessary complexity I can think of is to force
overloaded operators to be "arrow functions" to encourage only minimal
code, e.g.

operator +(Number $other, OperandPosition $operandPos): Number => return
new Number ($this->value + $other->value);

---

## #116625 — Larry Garfield — 2021-12-12T17:32:14+00:00

URL: https://externals.io/message/116611#116625 · In reply to: #116623

> 
>
> > […]
>
>
> I'm not strongly opinionated on either approach (magic __add vs operator +)
> although I have a very slight preference to your propose operator + syntax,
> however despite very occasionally thinking that this would be a useful
> feature, I always start to think about all the awful ways this could be
> used, making debugging and reasoning about code harder, since unexpected
> magic behaviour can (and probably will be) introduced.
>
> The proposal is technically reasonable, pending consideration of reflection
> and so on, but I just think the desire to use this in ways it shouldn't be
> used will be too great, and we'll end up with horrendous complexity.
> Perhaps I'm too cynical.
>
> The only mitigation for unnecessary complexity I can think of is to force
> overloaded operators to be "arrow functions" to encourage only minimal
> code, e.g.
>
> operator +(Number $other, OperandPosition $operandPos): Number => return
> new Number ($this->value + $other->value);

I don't think that would be possible.  As many of the examples in the RFC show, there are numerous cases where an operator function/callback/thing will need branching logic internally.  Even if we did that, people could just sub-call to a single function which would be just as complex as if it were in the operator callback directly.

(Though I still would like to see "short functions" in the language, despite it having been rejected once already.)

--Larry Garfield

---

## #116626 — Rowan Tommins — 2021-12-12T18:29:04+00:00

URL: https://externals.io/message/116611#116626 · In reply to: #116625

> > […]
> I don't think that would be possible.  As many of the examples in the RFC show, there are numerous cases where an operator function/callback/thing will need branching logic internally.  Even if we did that, people could just sub-call to a single function which would be just as complex as if it were in the operator callback directly.


I don't know if this would actually be helpful, but you could *force* 
the operator definition to be an alias for a normal method. That would 
(at least partially) solve the "dynamic call" problem, because the 
underlying method would be available with the existing dynamic call syntax.

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)  {}
}


Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #116627 — Dan Ackroyd — 2021-12-12T18:30:54+00:00

URL: https://externals.io/message/116611#116627 · In reply to: #116623

>
> The only mitigation for unnecessary complexity I can think of is to force
> overloaded operators to be "arrow functions" to encourage only minimal
> code, e.g.
>

The 'valid' use-cases for this aren't going to minimal pieces of code.

Things like a matrix object that supports multiplying by the various
things that matrices can be multiplied by won't fit into an arrow
function.

Also, I fundamentally disagree with making stuff difficult to use to
'punsish' users who want to use that feature. If you want to enforce
something like a max line length in a coding standard, or forbidding
usage of a feature, that is up to you and any code style tool you use.

> I just think the desire to use this in ways it shouldn't be
> used will be too great,

It might be an idea to add a list of bad examples to the RFC, so
people can refer to how not to use it, rather than each programming
team having to make the same mistakes.

cheers
Dan
Ack

---

## #116628 — Dan Ackroyd — 2021-12-12T18:56:32+00:00

URL: https://externals.io/message/116611#116628 · In reply to: #116621

>
> Using symbols, however, would (with some future extension to make it extensible) allow for:

I don't get how it's easier, other than being able to skip naming the
symbol name. e.g. adding union and intersection operators

function __union(...){}
function __intersection(...){}

vs

operator  ∪(...){}
operator ∩(...){}

In fact, I find one of those quite a bit easier to read...

Larry Garfield wrote:
> It uses effectively the same operator sigil, though.

Yes, that's what I was trying to say.

Danack wrote:
> The name of the function (e.g. __add) always refers to the symbol used
> where it is used, not what it is doing.

If the naming is taken from the sigil, then it's always appropriate.

So if operator * has the magic method __asterisk instead of __mul, it
avoids any suggestion of what the operation actually means for the
object.

btw, I don't really care about this naming problem. My concern is that
it's being used as a reason for introducing a special new type
function, when it's really not a big enough problem to deserve making
the language have special new syntax.

cheers
Dan
Ack

---

## #116629 — Rowan Tommins — 2021-12-12T18:58:08+00:00

URL: https://externals.io/message/116611#116629 · In reply to: #116626

> 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]

---

## #116630 — Dan Ackroyd — 2021-12-12T19:10:25+00:00

URL: https://externals.io/message/116611#116630 · In reply to: #116622

>
> I *think* that all of the things you mentioned will need similar
> updates to work correctly with this RFC even if it was done
> with plain old magic methods instead.

No, that's not true. Codesniffer and all the other tools parse magic
methods just fine. Stuff like the coverage notation for PHPUnit would
understand `@covers BigNumber::__plus` just fine.

The main piece of work each of them would need to do to support this
RFC, if it was based on magic methods, is being able to understand
that objects can work with operators:

```
$foo = new BigNumber(5);
$foo + 5; // Check that BigNumber implements the magic method __plus
```

That is far less work than having to add stuff to parse a new way to
declare functions.

Jordan LeDoux wrote:
>
> Danack wrote:
> > […]
>
> First, you can still get the closure for the operator implementation
> from Reflection if you really, really need it.

Sorry, but I just find that a bizarre thing to suggest. Introducing a
new type of function that can only be called in a particular way needs
to have really strong reasons for that, not "oh you can still call it
through reflection".

I think you've taken the position that using the symbols are cool, and
you're reasoning about how the RFC should operate from decision.

I'm not sure I can make a reasonable argument against it that you
would find persuasive, but to me it's adding a non-trivial amount of
complexity, which tips the RFC from being acceptable, to not.

cheers
Dan
Ack

---

## #116631 — Larry Garfield — 2021-12-12T22:01:48+00:00

URL: https://externals.io/message/116611#116631 · In reply to: #116628

> 
> > […]
>
> I don't get how it's easier, other than being able to skip naming the
> symbol name. e.g. adding union and intersection operators
>
> function __union(...){}
> function __intersection(...){}
>
> vs
>
> operator  ∪(...){}
> operator ∩(...){}
>
> In fact, I find one of those quite a bit easier to read...

If the list of operators is expanded by the engine, yes.  The point is that IF it were decided in the future to allow user-space defined operators, that would be considerably easier with a separate keyword.  Eg:

class Matrix {
  operator dot(Matrix $other, bool $left) {
    // ...
  }
}

$result = $m1 dot $m2;

Whether that is something we want to do is another question, but the operator keyword makes that logistically easy, while using __mul or __astrisk makes it logistically hard.

Using an attribute instead to bind a method to an operator, as previously suggested, would also have that flexibility if we ever wanted it.  There seems to be a lot of backpressure against using attributes for such things, though, and it wouldn't cleanly self-resolve the issue of keywords that make sense on methods being nonsensical on operators.  (public, static, etc.).  I'd probably be fine with it myself, but I cannot speak for others.

--Larry Garfield

---

## #116632 — Rowan Tommins — 2021-12-12T22:20:45+00:00

URL: https://externals.io/message/116611#116632 · In reply to: #116631

> If the list of operators is expanded by the engine, yes.  The point is that IF it were decided in the future to allow user-space defined operators, that would be considerably easier with a separate keyword.


A real-life example of this approach would be PostgreSQL, where a 
user-defined operator can be (almost) any combination of + - * / < > = ~ 
! @ # % ^ & | ` ?

It would be *possible* to have an open-ended naming scheme for these, 
such as "function __atSign_leftAngle" for the operator @> (which 
conventionally means "contains" in PostgreSQL) but it would be rather 
awkward compared to "operator &>" or "#[Operator('&>')]".


Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #116633 — Jordan LeDoux — 2021-12-13T07:17:59+00:00

URL: https://externals.io/message/116611#116633 · In reply to: #116628

Danack wrote:

> btw, I don't really care about this naming problem. My concern is that
> it's being used as a reason for introducing a special new type
> function, when it's really not a big enough problem to deserve making
> the language have special new syntax.
>
>
Danack wrote:
> I think you've taken the position that using the symbols are cool, and
> you're reasoning about how the RFC should operate from decision.

Ah, I see. That's a more fundamental objection than the technicals, I
think. It sort of implies that any arguments I provide are justifications
rather than arguments, which makes it difficult to have a productive
conversation about it. You expressed a similar concern about your efforts
to present arguments to me, which makes sense if this is your fundamental
concern.

First, let me start off by saying that I fully acknowledge and document in
the RFC that it is possible to provide a perfectly workable version of
*this* RFC without the operator keyword. I mention as much in the RFC. If
that is a true blocker for voters, I would at least consider it. However, I
do believe that's the incorrect decision. Not because it's "cool". The code
that handles the parsing of the new keyword is the only part of this RFC
that I didn't write from scratch, it was contributed by someone more
familiar with the parser. I feel like I could hardly have the "coolness" of
the work being my motivating factor when I did not in fact write that part
of the code.

But I do understand the concern. Adding complexity without reason is
foolish, particularly on a project that impacts many people and is
maintained by volunteers. As I immediately told you, I don't think your
concern is without merit, and I don't think it's something that should be
dismissed. But I clearly have (still) done a poor job communicating what I
perceive as the factors that outweigh this concern. It's not that I think
the concern is invalid or that it's small, it's that I view other things as
being an acceptable tradeoff. So I'll attempt one more time to communicate
why.

# Forwards Compatibility

Other replies have touched on this, and the RFC talks about this too, but
perhaps the language used has been skipping a couple of steps. This is, by
far, the biggest driving factor for why I believe the operator keyword is
the correct decision, so I will spend most of my time here.

There are two main kinds of forward compatibility achieved with a new
keyword that are difficult to achieve with magic methods: compatibility
with arbitrary symbol combinations, and behavior modifiers that can be
scoped only to operators. You mention that the symbols could be replaced
with their symbol names in english, which avoids the issue of misnaming the
functions. But this would still require the engine to specifically support
every symbol combination that is allowed.

Now, in this RFC I am limiting overloads to *not only* symbols which are
already used, but to a specific subset of them which are predetermined.
This is for several reasons:

1. The PHP developer community will have no direct experience with operator
overloads unless they have experience with another language such as C# or
python which supports them. Giving developers an initial set of operators
that address 90% of use cases but are limited allows the PHP developer
community time to learn and experiment with the feature while avoiding some
of the most dangerous possible misuses, such as accidentally redefining the
&& operator in a way that breaks boolean algebra.
2. This reduces the change necessary to the VM, to class entries, and to
the behavior of existing opcodes. This PR is already very large, and I
wanted to make sure that it wasn't impossible for the people who
participate here on their own time to actually consider the changes being
suggested.
3. I am already aware of several people within internals that believe any
version of this feature will result in uncontrolled chaos in PHP codebases.
I think this is false, as I do not see that kind of uncontrolled chaos in
the languages which do have this feature. However I would think that
allowing arbitrary overloads would increase that proportion.
4. This is limited to operator combinations with objects, which *ALL*
currently result in an error. That means there is no code that was working
on PHP 8.1 that will break with this included, as all such code currently
results in a fatal error. The current error is even the parent class of the
error *after* this RFC, so even the catch blocks, if they currently exist
in PHP codebases, should continue to work as before.

However, once a feature is added it is very difficult to change it. Not
only for backward compatibility reasons, but for the sheer inertia of the
massive impact that PHP has. I do not plan on ever proposing that arbitrary
symbol combinations be allowed for overloads myself. But I cannot possibly
know what internals might think of that possibility 10 years from now when
this feature has been in widespread usage for a long time. Using magic
methods makes it extremely difficult at *any* point in the future to allow
PHP developers the option of an overload for say +=+. What would such a
magic method be? __plus_equals_plus()? With some kind of magic in the
compiler to rename symbols in certain circumstances?

That sounds far *less* maintainable to me. It seems more likely that even
if it were a desired feature 10 years from now, it would be something that
would be extremely difficult to implement, maintain, and pass.

I also elaborate in the RFC as to why I think allowing operator specific
method modifiers is a very powerful bit of forwards compatibility as well.
Method modifiers simply result in a change to the function flags mask,
which is an extremely low cost lookup, which makes it very easy to
implement such features in the future if they are desired. I want to make
sure that once included, this feature doesn't result in a dead-end
implementation that boxes internals out of improvements that can be made
moving forward. I think that this is something that is far easier to do
with the operator keyword than it is with magic methods.

# Code That Promotes Correct Usage

Enums, as an example, are classes. Internally, they are classes in most
respects. So why is a new keyword for enums useful? Not only for many of
the same reasons listed above, but also because it is *useful* for the
language to communicate to the developer that a certain thing should be
treated differently, even if it shares a syntax. The fact that PHP
developers can *see* that enums are different from classes in their code is
not a trivial and unimportant matter.

In the same way, operator overloads are methods. Internally, they are
methods in most respects. But it is *useful* for the language to
communicate that *these* methods will change engine behavior. It is
*useful* for it to communicate that they should be treated differently. The
fact that PHP developers will be able to see that operators are different
from methods will help avoid some of the concerns people have with misuse.
It will communicate that these are areas where new maxims and new habits
should apply, that new things must be learned and new rules followed.

This may seem like such an esoteric suggestion to some, but it follows from
an entire field of study: human-centered design. This is a rigorous field
which explores how technology can be *designed* to be used correctly.

# Acceptance Of Restrictions

We can, of course, place restrictions on how operator overloads are used
when we are concerned about causing trouble. But such restrictions will
generate frustration and opposition in some circumstances. Enums are
another great example. Methods on enums are simply not allowed to do things
that will mutate the object. The engine simply prohibits it. This makes a
lot of sense for enums, but would such restrictions be possible if enums
were simply classes which have cases within them? Technically, certainly it
would be possible. But while I do not hear a lot of PHP developers
complaining about having method behavior restricted in enums, I expect that
there would be a lot of this unnecessary noise if instead PHP developers
saw them as "classes which have cases".

The fact that they are marked as a distinct construct simply makes such
restrictions make more sense to the people who use them.

These are engine hooks. People should not be shoving lots of other logic
into operator overloads. They should always be returning a result, they
should nearly always be implemented immutably, they should document the
logic of interaction with the given operator and nothing more. They
*shouldn't* be directly called, because they should not contain the kind of
logic that you *want* to directly call.

One of these restrictions that I included in this RFC was that typing the
parameters is not optional. This is extremely useful for operator
overloads, because you must document all the types that your implementation
understands how to interact with, and the engine will simply not allow for
undetermined or uncertain values to be handled. This restriction would feel
very out of place to many in a function, because other PHP functions do not
behave this way. But for a new thing, with a new keyword that marks itself
as something separate? Well now it makes sense. New things have their own
rules. Just like the restrictions on enum classes.

--

I think these things outweigh the cost of adding a new keyword,
particularly a new keyword that is limited only to the class definition and
that has behavior and syntax that is substantially similar to something
developers are already familiar with. I truly believe this is the better
way of doing this feature, I would not suggest it otherwise. And while an
implementation that doesn't include this is possible and workable, I feel
it is suboptimal and limiting. I feel that it is more likely to result in
problematic usage, complaints, and buggy code from PHP developers.

This new keyword required very minimal changes to the parser, and no
changes to the compiler. I think this is an acceptable tradeoff for the
benefits it brings. That is the reason that I am arguing for it, and no
other reason. I'm sorry if it seems like I am not listening to what you are
saying. That is not the case, I take the feedback of others on this list
very seriously. It's just that you haven't yet brought up a point which I
haven't considered and personally decided was worth the benefits. I agree
this will result in changes for tooling. I accept that those changes will
be larger with a new keyword. I do not think that it is worth delivering an
inferior version of this feature that is more prone to error and misuse,
and is more restricted in future scope.

Jordan

---

## #116634 — Stanislav Malyshev — 2021-12-13T08:01:50+00:00

URL: https://externals.io/message/116611#116634 · In reply to: #116633

Hi!

> 3. I am already aware of several people within internals that believe any
> version of this feature will result in uncontrolled chaos in PHP codebases.
> I think this is false, as I do not see that kind of uncontrolled chaos in
> the languages which do have this feature. However I would think that
> allowing arbitrary overloads would increase that proportion.

Depends on how you define "uncontrolled chaos". I have encountered 
toolkits where the authors think it's cute to define "+" to mean 
something that has nothing to do with mathematical addition (go read the 
manual for an hour to figure what it actually does) or for += to mean 
something different than + and assignment. Some people even learn to 
love it, so far I haven't.

> However, once a feature is added it is very difficult to change it. Not
> only for backward compatibility reasons, but for the sheer inertia of the
> massive impact that PHP has. I do not plan on ever proposing that arbitrary
> symbol combinations be allowed for overloads myself. But I cannot possibly
> know what internals might think of that possibility 10 years from now when
> this feature has been in widespread usage for a long time. Using magic
> methods makes it extremely difficult at *any* point in the future to allow
> PHP developers the option of an overload for say +=+. What would such a

That's awesome. The only thing worse than a toolkit author that thinks 
it's cute to play with "+" is the one that thinks it's cute to invent 
some random combination of special characters and assign it some meaning 
that of course is obvious to the author, but unfortunately that's the 
only person in existence to whom is it obvious. Some people enjoy the 
code being a puzzle that you need to untangle and make a sherlockian 
detective work to even begin to understand what is going on in this 
code. Other people have work to do. And again, what's the intuitive 
difference between operators +=+@-+ and ++--=!* ?

Of course, of course I know, every feature can be abused. The difference 
here is that if it's hard to think about any use that wouldn't be an abuse.

> That sounds far *less* maintainable to me. It seems more likely that even
> if it were a desired feature 10 years from now, it would be something that
> would be extremely difficult to implement, maintain, and pass.

I must notice "if" carries a lot of load here.
-- 
Stas Malyshev
smalyshev@gmail.com

---

## #116649 — Dik Takken — 2021-12-15T15:03:40+00:00

URL: https://externals.io/message/116611#116649 · In reply to: #116611

> Hello internals,
> 
> I last brought this RFC up for discussion in August, and there was
> certainly interesting discussion. Since then there have been many
> improvements, and I'd like to re-open discussion on this RFC. I mentioned
> in the first email to the list that I was planning on taking a while before
> approaching a vote, however the RFC is much closer to vote-ready now, and
> I'd like to open discussion with that in mind.
> 
> RFC Link: https://wiki.php.net/rfc/user_defined_operator_overloads
> 

Hi Jordan,

Thanks a lot for your work on this RFC! I like the direction this is going.

One thing that may be worthwhile looking into is the query builder use 
case. I mentioned it before:

https://externals.io/message/115648#115771

Basically it would enable using plain PHP expressions in stead of 
strings. So in stead of

     $query->where('product.price < ?1')->setParameter(1, 100);

one could write:

     $query->where(Price < 100);

Here Price is a class that represents a database column which has a 
(static) overload of the '<' operator. The operator overload yields an 
object representing a database expression, which gets passed to the 
where() method.

In general I don't like this sort of clever construct which makes one 
wonder what on earth is going on. The reason I do like this particular 
use case is that it can simplify code and enable static analysis of 
query expressions.

Now I'm not suggesting to support this creative use of operator 
overloading in the current RFC. It may however be useful to consider if 
this use case could be supported by a future RFC in a backward 
compatible way. Perhaps the RFC could mention it as a possible future 
extension.

Kind regards,
Dik Takken

---

## #116650 — Rowan Tommins — 2021-12-15T16:10:58+00:00

URL: https://externals.io/message/116611#116650 · In reply to: #116649

> $query->where(Price < 100);
>
> Here Price is a class that represents a database column which has a 
> (static) overload of the '<' operator. The operator overload yields an 
> object representing a database expression, which gets passed to the 
> where() method. 


The biggest problem with this particular example is not the operator 
overloading, but the bare word "Price", which is currently a constant 
lookup, not a class reference, as in:

const Price = 50;
var_dump(Price < 100);


However, with any version of operator overloading that didn't limit the 
return values of the overloaded operator, you could do something like:

$query->where(Product::$price < 100)

Where the static property Product::$price is an object which overloads 
the "<" operator to return some kind of Condition object which can be 
used by the query builder.


Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #116651 — Larry Garfield — 2021-12-15T21:18:14+00:00

URL: https://externals.io/message/116611#116651 · In reply to: #116650

> 
> > […]
>
>
> The biggest problem with this particular example is not the operator 
> overloading, but the bare word "Price", which is currently a constant 
> lookup, not a class reference, as in:
>
> const Price = 50;
> var_dump(Price < 100);
>
>
> However, with any version of operator overloading that didn't limit the 
> return values of the overloaded operator, you could do something like:
>
> $query->where(Product::$price < 100)
>
> Where the static property Product::$price is an object which overloads 
> the "<" operator to return some kind of Condition object which can be 
> used by the query builder.

Cool as that would be, it poses a problem as it would mean the Price object could either be directly compariable, or query-builder-comparable, but not both.  There's no way to have multiple <=> overrides in different contexts.

Also, for <=> in particular, that one is restricted to only return -1 | 0 | 1 anyway, so it wouldn't be able to return a query builder object.

--Larry Garfield

---

## #116652 — Jordan LeDoux — 2021-12-16T04:01:50+00:00

URL: https://externals.io/message/116611#116652 · In reply to: #116649

> Hi Jordan,
>
> Thanks a lot for your work on this RFC! I like the direction this is going.
>
> One thing that may be worthwhile looking into is the query builder use
> case. I mentioned it before:
>
> https://externals.io/message/115648#115771
>
> Basically it would enable using plain PHP expressions in stead of
> strings. So in stead of
>
>      $query->where('product.price < ?1')->setParameter(1, 100);
>
> one could write:
>
>      $query->where(Price < 100);
>
> Here Price is a class that represents a database column which has a
> (static) overload of the '<' operator. The operator overload yields an
> object representing a database expression, which gets passed to the
> where() method.
>
> In general I don't like this sort of clever construct which makes one
> wonder what on earth is going on. The reason I do like this particular
> use case is that it can simplify code and enable static analysis of
> query expressions.
>
> Now I'm not suggesting to support this creative use of operator
> overloading in the current RFC. It may however be useful to consider if
> this use case could be supported by a future RFC in a backward
> compatible way. Perhaps the RFC could mention it as a possible future
> extension.
>
> Kind regards,
> Dik Takken
>

This is not a use case I highlighted because it's one that would be
difficult to support with this RFC. But as you say, it could be a good
future expansion. In particular, putting a query builder object into core
with some more advanced overloads built in may be the best way to
accomplish this, particularly if it is built with the idea in mind that the
entities themselves may also have overloads.

I can certainly add it to the future scope of this RFC however.

--

RE: The operator keyword and operator implementations being non-callable.

This was a limitation that I purposely placed on the operator keyword, as I
didn't like the idea of allowing syntax of the style `$obj->{'+'}();` or
`$obj->$op();` and I wanted developers to clearly understand that they
shouldn't treat these as normal methods in the vast majority of
circumstances. However, it seems this is one of the largest sticking points
for those who would otherwise support the RFC. To that end, I'm considering
removing that restriction on the `operator` keyword. If I were to do that,
you'd no longer need to wrap the operator in a closure to call it, though
the parser would still have problems with `$obj->+(...);`

I suppose my question then would be, is this an acceptable compromise on
the operator keyword? It removes one of the more annoying hurdles that
Danack mentioned and that others have pointed out, but retains much of the
benefits of the keyword that I expressed in my last email.

Jordan

---

## #116653 — Pierre — 2021-12-16T08:23:47+00:00

URL: https://externals.io/message/116611#116653 · In reply to: #116652

Le 16/12/2021 à 05:01, Jordan LeDoux a écrit :
> This is not a use case I highlighted because it's one that would be
> difficult to support with this RFC. But as you say, it could be a good
> future expansion. In particular, putting a query builder object into core
> with some more advanced overloads built in may be the best way to
> accomplish this, particularly if it is built with the idea in mind that the
> entities themselves may also have overloads.
>
> I can certainly add it to the future scope of this RFC however.
>
> --
>
> RE: The operator keyword and operator implementations being non-callable.
>
> This was a limitation that I purposely placed on the operator keyword, as I
> didn't like the idea of allowing syntax of the style `$obj->{'+'}();` or
> `$obj->$op();` and I wanted developers to clearly understand that they
> shouldn't treat these as normal methods in the vast majority of
> circumstances. However, it seems this is one of the largest sticking points
> for those who would otherwise support the RFC. To that end, I'm considering
> removing that restriction on the `operator` keyword. If I were to do that,
> you'd no longer need to wrap the operator in a closure to call it, though
> the parser would still have problems with `$obj->+(...);`
>
> I suppose my question then would be, is this an acceptable compromise on
> the operator keyword? It removes one of the more annoying hurdles that
> Danack mentioned and that others have pointed out, but retains much of the
> benefits of the keyword that I expressed in my last email.
>
> Jordan

Hello,

I'm not an internals hacker nor someone who can vote, but here is my 
opinion about operator overloading: I don't like it. Nevertheless, if it 
has to be done, I'd like it to be less challenging for PHP newcomers or 
everyday developers.

An operator is not much more than a function shortcut, gmp examples 
prove that point quite well. I don't see why it is necessary to create a 
new syntax. It seems in the discussion that the magic method ship has 
sailed, but I'd much prefer it.

I don't see why a user wouldn't be able to call an operator 
method/function outside of the operator context. It has a signature: 
left operand and right operand are its parameters, and it has a return 
type. The engine internally will just call this function as userland 
code could do.

I think that adding a new syntax for it, and allowing weird function 
names which are the operator symbols will probably create some mind fuck 
in people's mind when reading the code. I like things being simple, and 
I'd love operator overloads to be simple functions, no more no less, not 
"a new thing". The more PHP syntax grows the more complex it is to learn 
and read.

Regarding the naming debate about operator names being different 
depending upon the context, I agree, but I don't care, if operators have 
a name in the engine, I'd prefer methods to carry the same name even if 
it yields a different name in the domain semantics of the object: if you 
are the low level operator function developer, you know what you are 
doing furthermore you can still comment code. If you're the end user, 
you will read the domain API documentation, not the code itself in many 
cases.

If the names are a problem, why not registering those using an attribute 
? If I remember well it was mentioned somewhere in the mail thread, it 
would provide a way to explicitly register any method, with any name, as 
being an operator implementation, sus userland could keep both syntax 
and use the one they wish (i.e. $newNumber = $number->add($otherNumber); 
or $newNumber = $number + $otherNumber. It's not insane to keep this 
possibility open, on the contrary, it'd leverage the fact that operator 
overloading in some context is just a shiny eye candy way of writing 
some domain function shortcut.

That's my opinion and I don't if it worth a penny, but in my mind, an 
operator is a function, and there's no reason that it'd be a different 
thing.

Regards,

--

Pierre

---

## #116654 — Andreas Hennings — 2021-12-16T12:21:17+00:00

URL: https://externals.io/message/116611#116654 · In reply to: #116653

Hello internals,
some concerns I have about operator overloading.
(I have seen and played with operator overloading long time ago in
C++, this is my background for these points.)

----

1. Searchable names.
Methods and functions have searchable and clickable names. Operators don't.
The "searchable" applies to grep searches in code, but also google
searches for documentation and support.
This adds to the concerns already raised by others, that we will see
arbitrary operators "just because we can".

----

2. Symmetry
Operators like "+" or "==" are often expected to be symmetric / commutative.
(for "*" I would not universally expect this, e.g. matrix
multiplication is not symmetric)
https://en.wikipedia.org/wiki/Commutative_property
https://en.wikipedia.org/wiki/Symmetric_function
Having one side of the operator "own" the implementation feels wrong,
and could lead to problems with inheritance down the line.

From C++ I remember that at the time, there was a philosophy of
defining and implementing these kinds of operations outside of the
objects that hold the data.

----

3. Lack of real parameter overloading
Unlike C++ (or C?), we don't have real method/function overloading
based on parameters.
I also don't see it being added in this operator RFC (and I would
disagree with adding it here, if we don't add it for functions/methods
first).
We have to solve this with inheritance override, and with if ($arg
instanceof ...) in the implementation.
What this does not give us is conditional return types:

This is what I would do in a language with parameter-based overloading:

class Matrix {
  operator * (Matrix $other): Matrix {..}
  operator * (float $factor): Matrix {..}
  operator * (Vector $vector): Vector {..}
}

class Vector {
  operator * (Vector $vector): float {..}
  operator * (float $factor): Vector {..}
}

(Or if we also have templates/generics, we could put dimension
constraints on the types, so that we cannot multiply matrices where
dimensions mismatch.)

Without real parameter overloading, we have to use instanceof instead,
and we cannot have the conditional type hints.


-- Andreas




>
> Le 16/12/2021 à 05:01, Jordan LeDoux a écrit :
> > […]
>
> Hello,
>
> I'm not an internals hacker nor someone who can vote, but here is my
> opinion about operator overloading: I don't like it. Nevertheless, if it
> has to be done, I'd like it to be less challenging for PHP newcomers or
> everyday developers.
>
> An operator is not much more than a function shortcut, gmp examples
> prove that point quite well. I don't see why it is necessary to create a
> new syntax. It seems in the discussion that the magic method ship has
> sailed, but I'd much prefer it.
>
> I don't see why a user wouldn't be able to call an operator
> method/function outside of the operator context. It has a signature:
> left operand and right operand are its parameters, and it has a return
> type. The engine internally will just call this function as userland
> code could do.
>
> I think that adding a new syntax for it, and allowing weird function
> names which are the operator symbols will probably create some mind fuck
> in people's mind when reading the code. I like things being simple, and
> I'd love operator overloads to be simple functions, no more no less, not
> "a new thing". The more PHP syntax grows the more complex it is to learn
> and read.
>
> Regarding the naming debate about operator names being different
> depending upon the context, I agree, but I don't care, if operators have
> a name in the engine, I'd prefer methods to carry the same name even if
> it yields a different name in the domain semantics of the object: if you
> are the low level operator function developer, you know what you are
> doing furthermore you can still comment code. If you're the end user,
> you will read the domain API documentation, not the code itself in many
> cases.
>
> If the names are a problem, why not registering those using an attribute
> ? If I remember well it was mentioned somewhere in the mail thread, it
> would provide a way to explicitly register any method, with any name, as
> being an operator implementation, sus userland could keep both syntax
> and use the one they wish (i.e. $newNumber = $number->add($otherNumber);
> or $newNumber = $number + $otherNumber. It's not insane to keep this
> possibility open, on the contrary, it'd leverage the fact that operator
> overloading in some context is just a shiny eye candy way of writing
> some domain function shortcut.
>
> That's my opinion and I don't if it worth a penny, but in my mind, an
> operator is a function, and there's no reason that it'd be a different
> thing.
>
> Regards,
>
> --
>
> Pierre
>
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php

---

## #116656 — Dan Ackroyd — 2021-12-16T18:20:27+00:00

URL: https://externals.io/message/116611#116656 · In reply to: #116654

> Methods and functions have searchable and clickable names. Operators don't.
> The "searchable" applies to grep searches in code, but also google

That's one of the reasons why I prefer a magic methods based approach.

function __plus(...){}

can be searched for...and for future scope, something like:

function __union(...){}

is more self-documenting (imo) than:

operator  ∪(...){}


> Lack of real parameter overloading
> Unlike C++ (or C?), we don't have real method/function overloading
> based on parameters.

Java is probably a better comparison language than C.

I have a note on the core problem that method overloading would face
for PHP here: https://phpopendocs.com/rfc_codex/method_overloading

But although they both involved the word 'overloading', operator
overloading, and method overloading are really separate features.

> we cannot have the conditional type hints.

btw you can just say 'types'.

Unlike some lesser languages, in PHP parameter types are enforced at
run-time; they aren't hints. I believe all references to hints (in
relation to types at least) have been removed from the PHP manual.

cheers
Dan
Ack

---

## #116657 — Dan Ackroyd — 2021-12-16T18:30:43+00:00

URL: https://externals.io/message/116611#116657 · In reply to: #116653

>
>
> If the names are a problem, why not registering those using an attribute
> ?

If there is a strong reason to use attributes, then the argument
should start from there.

Starting from "well we could just use an attribute" and then putting
the pressure on other people to find a reason to _not_ use an
attribute, is a terrible design process.

Every language that has annotations ends up with far too many of them;
PHP is likely to end up with too many of them also. The time to push
back against using them is now, not when the damage has been done.

But to repeat, I don't think the names of magic methods are a problem.
Documenting that 'the name refers to the operator sigil, not to what
the function does', avoids it being a problem to be solved.

cheers
Dan
Ack

---

## #116660 — Andreas Hennings — 2021-12-16T19:24:57+00:00

URL: https://externals.io/message/116611#116660 · In reply to: #116656

>
> 
>
> > […]
>
> That's one of the reasons why I prefer a magic methods based approach.
>
> function __plus(...){}
>
> can be searched for...and for future scope, something like:
>
> function __union(...){}
>
> is more self-documenting (imo) than:
>
> operator  ∪(...){}
>

I don't mind using magic methods for this, compared to an operator keyword.
It is also what I found is happening in python.

However, this does not give us searchability in the calling place,
only where it is declared / implemented.


>
> > […]
>
> Java is probably a better comparison language than C.
>
> I have a note on the core problem that method overloading would face
> for PHP here: https://phpopendocs.com/rfc_codex/method_overloading
>
> But although they both involved the word 'overloading', operator
> overloading, and method overloading are really separate features.

I see the distinction in overloading based on the object type on the
left, vs overloading based on parameter types.

For a method call $a->f($b), the implementation of ->f() is chosen
based on the type of $a, but not $b.
For an operator call "$a + $b", with the system proposed here, again,
the implementation of "+" will be chosen based on the type of $a, but
not $b.
For native operator calls, the implementation is chosen based on the
types of $a and $b, but in general they are cast to the same type
before applying the operator.
For global function calls f($a, $b), the implementation is always the same.

In a language with parameter-based overloading, the implementation can
be chosen based on the types of $a and $b.

This brings me back to the "symmetry" concern.
In a call "$a->f($b)", it is very clear that the implementation is owned by $a.
However, in an operator expression "$a + $b", it looks as if both
sides are on equal footing, whereas in reality $a "owns" the
implementation.

Add to this that due to the weak typing and implicit casting,
developers could be completely misled by looking at an operator
invocation, if a value (in our case just the left side) has an
unexpected type in some edge cases.
Especially if it is not clear whether the value is a scalar or an object.
With a named method call, at least it is constrained to classes that
implement a method with that name.

>
> > […]
>
> btw you can just say 'types'.
>
> Unlike some lesser languages, in PHP parameter types are enforced at
> run-time; they aren't hints. I believe all references to hints (in
> relation to types at least) have been removed from the PHP manual.

Ok, what I mean is return type declarations.
In a class Matrix, operator(Matrix $other): Matrix {} can be declared
to always return Matrix, and operator(float $factor): float {} can be
declared to always return float.
However, with a generic operator(mixed $other): Matrix|float {}, we
cannot natively declare when the return value will be Matrix or float.
(a tool like psalm could still do it)

But even for parameters, if I just say "type" it won't be clear if I
mean the declared type on the parameter, or the actual type of the
argument value.


>
> cheers
> Dan
> Ack

---

## #116666 — Larry Garfield — 2021-12-16T23:24:22+00:00

URL: https://externals.io/message/116611#116666 · In reply to: #116660

> I see the distinction in overloading based on the object type on the
> left, vs overloading based on parameter types.
>
> For a method call $a->f($b), the implementation of ->f() is chosen
> based on the type of $a, but not $b.
> For an operator call "$a + $b", with the system proposed here, again,
> the implementation of "+" will be chosen based on the type of $a, but
> not $b.
> For native operator calls, the implementation is chosen based on the
> types of $a and $b, but in general they are cast to the same type
> before applying the operator.
> For global function calls f($a, $b), the implementation is always the same.
>
> In a language with parameter-based overloading, the implementation can
> be chosen based on the types of $a and $b.
>
> This brings me back to the "symmetry" concern.
> In a call "$a->f($b)", it is very clear that the implementation is owned by $a.
> However, in an operator expression "$a + $b", it looks as if both
> sides are on equal footing, whereas in reality $a "owns" the
> implementation.
>
> Add to this that due to the weak typing and implicit casting,
> developers could be completely misled by looking at an operator
> invocation, if a value (in our case just the left side) has an
> unexpected type in some edge cases.
> Especially if it is not clear whether the value is a scalar or an object.
> With a named method call, at least it is constrained to classes that
> implement a method with that name.

The RFC covers all of this, and the way it works around it.  Absent method overloading (which I don't expect any time soon, especially given how vehemently Nikita is against it), it's likely the best we could do.


> In a class Matrix, operator(Matrix $other): Matrix {} can be declared
> to always return Matrix, and operator(float $factor): float {} can be
> declared to always return float.
> However, with a generic operator(mixed $other): Matrix|float {}, we
> cannot natively declare when the return value will be Matrix or float.
> (a tool like psalm could still do it)

I... have no idea what you're talking about here.  The RFC as currently written is not a "generic operator".  It's

operator *(Matrix $other, bool $left): Matrix

The implementer can type both $other and the return however they want.  That could be Matrix in both cases, or it could be Matrix|float, or whatever.  That's... the same as every other return type we have now.

--Larry Garfield

---

Next page: https://externals.io/message/116611.md?page=2
