Re: Revisiting Userland Operator Overloads

From: Date: Mon, 23 Aug 2021 09:12:27 +0000
Subject: Re: Revisiting Userland Operator Overloads
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-115771@lists.php.net to get a copy of this message
On 08-08-2021 09:41, Jordan LeDoux wrote: > Off the top of my head here are some of the use cases that I think benefit > greatly from this: > > - Complex number objects > - Fractions objects > - Matrix objects (math) > - Vector objects (math) > - Time/Date intervals > - Collections > - Arbitrary precision numbers (which are usually represented as a string in > PHP) Hi Jordan, I would like to point out another interesting use case that I have seen in the Python world: Query builders. In that use case, operator overloads are used to create database filtering expressions for example, allowing one to write: db.products.filter(db.products.price < 100) Here db.products.price is an object representing a database column implementing the __lt__ method, overloading the < operator. This method returns an object representing a database filtering expression, which gets passed to the filter() method. So basically, this would allow for writing expressions in plain PHP and have them translated into other languages under the hood. The use case does require that operator overloads are allowed to return any kind of value they wish. Regards, Dik Takken

« previous php.internals (#115771) next »