RE: [PHP-DEV] Operator overloading for userspace objects

This is only part of a thread. view whole thread
  108333
January 30, 2020 21:22 jan.h.boehmer@gmx.de
> Unfortunately, this implementation goes in the wrong direction: PHP already has full internal support for operator overloading through the do_operation object handler. Operator overloading should be exposed to userland through that handler as well.
I have made another implementation (https://github.com/jbtronics/php-src/tree/operator_overloading) which provides an standard handler for do_operation, which calls the functions in user space. I also removed the __compare magic function from my implementation, so this can be handled separately. Another thing: What are your opinions on overload the bitwise not operator (~)? My implementations offers every other bitwise operator and this one would complete the set of bitwise operators. On the other hand unary operators does not have much use for objects and maybe encourage people using them as "shortcut" for calling things on the object, where an explicit method call would make the intend function more clear. Regards, Jan
  108334
January 31, 2020 15:41 larry@garfieldtech.com ("Larry Garfield")
On Thu, Jan 30, 2020, at 3:22 PM, jan.h.boehmer@gmx.de wrote:
> > Unfortunately, this implementation goes in the wrong direction: PHP already has full internal support for operator overloading through the do_operation object handler. Operator overloading should be exposed to userland through that handler as well. > > I have made another implementation > (https://github.com/jbtronics/php-src/tree/operator_overloading) which > provides an standard handler for do_operation, which calls the > functions in user space. > > I also removed the __compare magic function from my implementation, so > this can be handled separately.
I cannot speak to the implementation details. From a design perspective, I am tentatively positive on operator overloading, with separate method for each operator, BUT, the big question for me is the rules around type compatibility. Can you only compare 2 of the same type? What about subclasses? Can that differ if a subclass overrides that method? What happens to commutativity then? Can you compare based on an interface? Examples: class Money { public function __add(Money $m) { ... } } class Dollar extends Money { public function__add(Money $m) { ... } } class Euro extends Money { } $m = new Money(5); $d = new Dollar(10); $e = new Euro(15); What should happen in each of these cases? $m + $d; $d + $m; $m + $e; $e + $m; $d + $e; Or similarly, is this allowed: interface Money { public function __add(Money $m); } There's a lot of very tricksy logic here to work out in terms of what makes sense to do before we could consider it. That logic may already have been figured out by another language (Python, C#, etc.) in which case I'm perfectly happy to steal their logic outright if it makes sense to do so. It's worth investigating before we go further to see what the traps are going to be. Also, I want to reiterate: Any of these operations MUST be designed to return a new value, never modify in place. These operators only make sense on value objects, not service objects, and value objects should be immutable. Which means that there is no __addEquals() method, just _add(), and we continue with that being isomorphic to $a = $a + $b;, the behavior of which is readily predictable. I've actually been wondering lately if we shouldn't create an entirely separate data structure for value objects that helps enforce that difference of expectation. (Similar to shapes in Hack, or records or structs in various other languages.)
> Another thing: What are your opinions on overload the bitwise not > operator (~)? My implementations offers every other bitwise operator > and this one would complete the set of bitwise operators. On the other > hand unary operators does not have much use for objects and maybe > encourage people using them as "shortcut" for calling things on the > object, where an explicit method call would make the intend function > more clear.
Frankly I'd avoid bitwise operators entirely for now. I'm not even sure how you'd use those... --Larry Garfield
  108335
January 31, 2020 15:55 ben@benramsey.com (Ben Ramsey)
> Also, I want to reiterate: Any of these operations MUST be designed to return a new value, never modify in place. These operators only make sense on value objects, not service objects, and value objects should be immutable.
I completely agree. This was the gist of my earlier comments. Maybe we should resurrect discussion of the immutable classes and properties RFC: https://wiki.php.net/rfc/immutability If we add the ability to specify immutability, then we can enforce in the engine that the left and right operands must be immutable. For example: public function __add(immutable $left, immutable $right); Cheers, Ben
  108419
February 6, 2020 20:29 chasepeeler@gmail.com (Chase Peeler)
On Fri, Jan 31, 2020 at 10:55 AM Ben Ramsey <ben@benramsey.com> wrote:

> > Also, I want to reiterate: Any of these operations MUST be designed to > return a new value, never modify in place. These operators only make sense > on value objects, not service objects, and value objects should be > immutable. > > I completely agree. This was the gist of my earlier comments. > > Maybe we should resurrect discussion of the immutable classes and > properties RFC: https://wiki.php.net/rfc/immutability > > If we add the ability to specify immutability, then we can enforce in the > engine that the left and right operands must be immutable. > > For example: > > public function __add(immutable $left, immutable $right); > > Cheers, > Ben > > Ideally, I don't think the items have to be immutable. Here is a silly
use-case: public function __add($left,$right){ $left->operatedOn++; $right->operatedOn++; return $left->value + $right->value; } However, given the nature of operator overloading, I think the users should EXPECT what they pass in will not be changed, unless they explicitly pass by reference. This means we'd have to "change the rules" for operator overloading magic methods, where objects are passed by value unless explicitly passed by reference ( public function __add(&$left, &$right) ). I think that is an even worse idea! So, I think you really have two options. Change the rules so that even objects are passed by value in this specific circumstance, and there is no ability to pass by reference. I still don't like this, because it changes the rules for only a specific scenario, but I think it's a better option than the one above, as well as a better option than allowing mutable objects 100% of the time - although, I'm not totally against spelling out in the documentation "Don't modify the items passed in or you'll get unexpected results!" The other options is the immutability RFC. This doesn't change the rules - it just adds a new rule. I don't see in that RFC, though, anything about the immutable type hints. That's really the only thing that I think is applicable to operator overloading. -- Chase Peeler chasepeeler@gmail.com
  108336
January 31, 2020 16:35 benjamin.morel@gmail.com (Benjamin Morel)
I like this whole operator overloading thing. I would probably use it in
brick/math <https://github.com/brick/math> and brick/money
<https://github.com/brick/money> to replace verbose `->plus()`,
`->multipliedBy()` etc. calls.

> Can you only compare 2 of the same type? What about subclasses? Can that differ if a subclass overrides that method? What happens to
commutativity then? Can you compare based on an interface? I think that this should behave exactly the same as if you replaced `$a + $b` with `$a->__add($b)` in your code. Nothing more, nothing less. The result will depend on whether you type-hinted your magic method or not.
> These operators only make sense on value objects, not service objects, and value objects should be immutable.
Indeed, we would need to make it clear that the operation must not modify the value of the current object, that should be effectively treated as immutable. Because it will probably be hard for the engine to enforce this, what about using the same convention I use in my libraries, i.e. `plus()` instead of `add()`, `dividedBy()` instead of `divide()`, etc.? This would translate to magic methods such as: __plus() __minus() __multipliedBy() __dividedBy() Benjamin
  108337
January 31, 2020 17:32 mike@newclarity.net (Mike Schinkel)
> On Jan 31, 2020, at 10:41 AM, Larry Garfield <larry@garfieldtech.com> wrote: > > I cannot speak to the implementation details. From a design perspective, I am tentatively positive on operator overloading, with separate method for each operator, BUT, the big question for me is the rules around type compatibility.
I have avoided commenting on this thread to see where it would lead. I have been surprised so many here are embracing operator overloading. My experience taught me operator overloading has been added to languages because "it seemed like a good idea at the time." But it is now considered to be harmful, by many: - https://www.quora.com/What-are-the-pitfalls-of-operator-overloading - https://cafe.elharo.com/programming/operator-overloading-considered-harmful/ - https://www.oreilly.com/library/view/sams-teach-yourself/0672324253/0672324253_ch21lev1sec4.html - https://en.wikipedia.org/wiki/Operator_overloading#Criticisms - https://www.jarchitect.com/QACenter/index.php?qa=53&qa_1=overload-operators-special-circumstances-defined-literals (Ruby's ability for developers to redefine the entire language is an especially chilling example of the concepts of operator overloading taken to the extreme: https://dev.to/jimsy/please-stop-using-ruby-4lf1) That said, I will not protest further if others still really feel strongly about adding operator overloading after reviewing those criticisms.
> Also, I want to reiterate: Any of these operations MUST be designed to return a new value, never modify in place. These operators only make sense on value objects, not service objects, and value objects should be immutable. > > Which means that there is no __addEquals() method, just _add(), and we continue with that being isomorphic to $a = $a + $b;, the behavior of which is readily predictable.
Immutability is a great feature from functional programming. But I think it is orthogonal to operator overloading as it would be (relatively) easy to implement an __add() method but how would PHP enforce that __add() would not be able to mutate state? Consider the following. How would __add() stop the mutating happening in $foo->bar->increment_ops()? class Bar { private $_op_count = 0; function increment_ops() { $this->_op_count++; } } class Foo { public $value; public $bar; function __construct( int $value ) { $this->value = $value; } function __add( Foo $foo ): Foo { $this->bar->increment_ops(); return new Foo( $this->value + $foo->value ); } } $foo = new Foo(10); $foo->bar = new Bar(); $foo = $foo + new Foo(5); echo $foo->value; I am sure it would be _possible_ to stop it, but I do not think it is trivial to architect nor implement. Given that I believe immutability would need to be implemented as an independent feature and not as an aspect of operator overloading. If we still want operator overloading and we want to force operator overloading to require immutability, I believe that means we would need an immutability RFC to be approved (and implemented?) before operator overloading requiring immutability could be approved. Something like this: class Bar { private $_op_count = 0; immutable function increment_ops() { global $foo; $foo = new Foo(); <====== Compile error! $this->_op_count++; <====== Compile error! } }
> I've actually been wondering lately if we shouldn't create an entirely separate data structure for value objects...
+1 for that. -Mike
  108338
January 31, 2020 18:35 ben@benramsey.com (Ben Ramsey)
> If we still want operator overloading and we want to force operator overloading to require immutability, I believe that means we would need an immutability RFC to be approved (and implemented?) before operator overloading requiring immutability could be approved. Something like this:
For reference, immutability has been proposed in the past, but I’m not sure where it landed. It looks like it fizzled out. * https://wiki.php.net/rfc/immutability * https://externals.io/message/94913 * https://externals.io/message/96919 * https://externals.io/message/97355 * https://externals.io/message/101890 * https://externals.io/message/81426 I agree with you that I think we need to accept an immutability RFC before operator overloading (requiring immutability) can be approved. I also believe operator overloading should require immutability. Cheers, Ben
  108340
February 1, 2020 00:03 larry@garfieldtech.com ("Larry Garfield")
On Fri, Jan 31, 2020, at 11:32 AM, Mike Schinkel wrote:
> > On Jan 31, 2020, at 10:41 AM, Larry Garfield <larry@garfieldtech.com> wrote: > > > > I cannot speak to the implementation details. From a design perspective, I am tentatively positive on operator overloading, with separate method for each operator, BUT, the big question for me is the rules around type compatibility. > > I have avoided commenting on this thread to see where it would lead. > I have been surprised so many here are embracing operator overloading. > > My experience taught me operator overloading has been added to > languages because "it seemed like a good idea at the time." But it is > now considered to be harmful, by many: > > - https://www.quora.com/What-are-the-pitfalls-of-operator-overloading > - > https://cafe.elharo.com/programming/operator-overloading-considered-harmful/ > - > https://www.oreilly.com/library/view/sams-teach-yourself/0672324253/0672324253_ch21lev1sec4.html > - https://en.wikipedia.org/wiki/Operator_overloading#Criticisms > - > https://www.jarchitect.com/QACenter/index.php?qa=53&qa_1=overload-operators-special-circumstances-defined-literals > > (Ruby's ability for developers to redefine the entire language is an > especially chilling example of the concepts of operator overloading > taken to the extreme: https://dev.to/jimsy/please-stop-using-ruby-4lf1) > > That said, I will not protest further if others still really feel > strongly about adding operator overloading after reviewing those > criticisms.
Those are valid points. (Hence my "tentatively.") Operator overloading is one of those features that when used well can be really really nice, but is really easy to use badly (in which case it's really really not nice). In all honesty, I'd probably be more excited about bringing back comparison overloading (__compare() and __equals()) than overriding arithmetic. Unless we could get some kind of bind operator, but that's probably asking for trouble. :-)
> > Also, I want to reiterate: Any of these operations MUST be designed to return a new value, never modify in place. These operators only make sense on value objects, not service objects, and value objects should be immutable. > > > > Which means that there is no __addEquals() method, just _add(), and we continue with that being isomorphic to $a = $a + $b;, the behavior of which is readily predictable. > > Immutability is a great feature from functional programming. But I > think it is orthogonal to operator overloading as it would be > (relatively) easy to implement an __add() method but how would PHP > enforce that __add() would not be able to mutate state?
Currently it cannot. That's another point of concern. We could at best document it and put "please please don't modify the object" in the docs, but that would probably work just as well as you think it would... All of this is pointing in the "we need a language construct for value objects" direction, which I believe would be highly useful but I don't know how we'd do it nicely. (Mainly, how to handle what PSR-7 does with withX() type methods, which are clunky but the best we can do without some really funky new syntax.) --Larry Garfield
  108341
February 1, 2020 09:22 nikita.ppv@gmail.com (Nikita Popov)
On Thu, Jan 30, 2020 at 10:22 PM boehmer@gmx.de> wrote:

> > Unfortunately, this implementation goes in the wrong direction: PHP > already has full internal support for operator overloading through the > do_operation object handler. Operator overloading should be exposed to > userland through that handler as well. > > I have made another implementation ( > https://github.com/jbtronics/php-src/tree/operator_overloading) which > provides an standard handler for do_operation, which calls the functions in > user space. >
Looks much better! If you submit a pull request, I can leave some more detailed comments.
> I also removed the __compare magic function from my implementation, so > this can be handled separately. > > Another thing: What are your opinions on overload the bitwise not operator > (~)? My implementations offers every other bitwise operator and this one > would complete the set of bitwise operators. On the other hand unary > operators does not have much use for objects and maybe encourage people > using them as "shortcut" for calling things on the object, where an > explicit method call would make the intend function more clear. >
Don't really see a reason not to support it. We do overload the bitwise not operator for GMP objects, and it would be equally applicable to other "integer" style objects. If you would like to start an RFC on this topic, please sign up for a wiki account (https://wiki.php.net/rfc/howto) and send me your username. Nikita
  108342
February 1, 2020 10:36 jan.h.boehmer@gmx.de
On Sat, Feb 1, 2020 10:22 AM ppv@gmail.com> wrote:
> Looks much better! If you submit a pull request, I can leave some more detailed comments.
Okay, I will submit a pull request with my changes.
> If you would like to start an RFC on this topic, please sign up for a wiki account (https://wiki.php.net/rfc/howto) and send me your username.
My Wiki account name is jbtronics. Thank you, Jan Böhmer