On March 28, 2020 5:44:28 PM GMT+01:00, Nikita Popov <nikita....@gmail.com> 
wrote:
>On Sat, Mar 28, 2020 at 5:28 PM Johannes Schlüter
><johan...@schlueters.de>
>wrote:
>
>> On Sat, 2020-03-28 at 17:05 +0100, Christoph M. Becker wrote:
>> > On 28.03.2020 at 15:57, Johannes Schlüter wrote:
>> >
>> > > On March 28, 2020 1:25:11 PM GMT+01:00, "Christoph M. Becker" <
>> > > cmbecke...@gmx.de> wrote:
>> > >
>> > > > This "try left/right" approach is how operator overloading
>works
>> > > > for
>> > > > internal classes[1], and apparently, it works quite well, as
>long
>> > > > as it
>> > > > is not overused.
>> > >
>> > > The fact that it works in one or two cases as an implementation
>> > > detail where the full implementation is controlled by a single
>> > > group (internals) is no indication for it to work at large.
>> >
>> > Fair enough.  But maybe Python, where userland operator overloading
>> > works similar to the proposal at hand, is? :)
>>
>> It doesn't:
>>
>>
>> Python 3.6.9 (default, Nov  7 2019, 10:44:02)
>> [GCC 8.3.0] on linux
>> Type "help", "copyright", "credits" or "license" for more
>information.
>> >>> class A:
>> ...     def __add__(self, other):
>> ...         print("add")
>> ...
>> >>> a = A()
>> >>> a + 1
>> add
>> >>> 1 + a
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> TypeError: unsupported operand type(s) for +: 'int' and 'A'
>>
>
>It does.
>
>Because Python uses instance methods for operator overloading, it has
>to
>use pairs of methods like __add__ and __radd__ to handle commuted
>variants.
>If you want to implement a commutative add, you need to implement both
>__add__ and __radd__. If __add__ is not implemented or returns
>NotImplemented, then __radd__ will be called.
>
>So, as Christoph has said, this is indeed the same system as what is
>being
>proposed here, with the difference that we do not need two separate
>methods
>per operator, but can use a static method instead.

If we use two methods as well it is a saner design. I won't like it, but lot 
better than the current one.

johannes

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

Reply via email to