When to opCall instead of opIndex and opSlice?

Steven Schveighoffer schveiguy at yahoo.com
Mon Oct 2 19:29:21 UTC 2017


On 10/2/17 2:31 PM, Per Nordlöw wrote:
> On Monday, 2 October 2017 at 18:14:24 UTC, Jacob Carlborg wrote:
>> On 2017-10-02 17:57, Nordlöw wrote:
>>> Is implementing opCall(size_t) for structures such as array 
>>> containers that already define opIndex and opSlice deprecated?
>>>
>>> I can't find any documentation on the subject on when opCall should 
>>> be defined to enable foreach (isIterable).
>>
>> opCall is not related to foreach. It's used to overload the call 
>> operator, i.e. ().
>>
>> struct Foo
>> {
>>     void opCall() {};
>> }
>>
>> Foo foo;
>> foo();
>>
>> Are you thinking of opApply [1]?
>>
>> [1] https://dlang.org/spec/statement.html#foreach_over_struct_and_classes
> 
> Ahh, yes of course.
> 
> It seems like defining opIndex anf opSlice is enough in the array 
> container case. Why do we have opApply as well? Non-random access?

First, it should be front, popFront, and empty, not opIndex and opSlice.

Second, opApply has existed forever (even in D1). Ranges are more recent.

Third, opApply has some characteristics that are difficult to implement 
via ranges.

-Steve


More information about the Digitalmars-d-learn mailing list