On 06/08/2021 17:34, Jordan LeDoux wrote:
1. The only supported operators in the RFC will be the mathematical
operators: (+, -, /, *, **, %)
2. The RFC would also provide an interface, (something like
MathObjectInterface), that has all the magic methods in it.
3. The do_operation would be changed to check for the interface being
implemented instead of the specific method.


Hi Jordan,

In a previous thread [1], I wrote about two fundamental design approaches to operator overloading:

a) Treating operators as arbitrary symbols, which can be assigned any operation which makes sense in a particular domain. b) Treating operators as having a fixed meaning, and allowing custom types to implement them with that meaning.

Both approaches have their pros and cons, but my general impression is that approach (b) has more support on this list.

For approach (b), I think your proposal to group operations into interfaces is a good one - it doesn't prevent someone using the operators to mean something completely different, but it expresses a clear opinion.

I think using "+" for a union of two collections is really an example of approach (a) - it doesn't really mean "add", and it's no less arbitrary than using "<<" to add an item to a collection (as is conventional in Ruby, for instance).  If we want to go down that route, and allow individual operators to be overloaded, I think we should look for a syntax that lets you pick the symbol, rather than giving them names which only make sense in some contexts.


[1] https://news-web.php.net/php.internals/108347

--
Rowan Tommins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to