Without D1-style operator overloads, the `in` operator for classes doesn't make sense

Steven Schveighoffer schveiguy at gmail.com
Mon Jan 6 14:14:35 UTC 2020


On 1/6/20 6:16 AM, Ontonator wrote:
> On Monday, 6 January 2020 at 10:55:15 UTC, Don wrote:
>> In DMD 2.088.0, the D1 operator overloads were deprecated, in favour 
>> of the D2 syntax which has been available for years.
>>
>> [...]
> 
> I’m on my phone right now, so I can’t check, but could you emulate a 
> virtual operator by creating a virtual method (e.g. `contains`) which is 
> called from the operator and which child classes could override? It 
> isn’t as ergonomic, but it should work.

In fact this is exactly why this change was allowed (and this exact 
problem was discussed):

class C
{
    int* opIn(string key); // virtual

    auto opBinaryRight(string op : "in", X)(X key) // wrapper
    {
        return opIn(key);
    }
}

So when/if the D1 operators get removed, you put this wrapper in, and it 
works.

In fact, you can easily make a mixin template that will implement all 
the D2 style operators when you have defined the D1 style ones.

-Steve


More information about the Digitalmars-d mailing list