Argumnentation against external function operator overloading is unconvincing

pineapple via Digitalmars-d digitalmars-d at puremagic.com
Sun Sep 25 06:32:23 PDT 2016


On Sunday, 25 September 2016 at 13:10:42 UTC, ZombineDev wrote:
> But which opIndex and which length? Those of the container, or 
> those of the range? It doesn't make any sense to expect 
> container[].takeExactly(7).length to be different than 7. If 
> range.length returns the length of the container this would 
> break every sensible algorithm out there. How would you even 
> implement binary search 
> (https://rosettacode.org/wiki/Binary_search#D)? It's completely 
> wrong to expect indexing operations to work on the original 
> container. What you do in situations where there is no 
> container, such as generators (e.g. iota 
> https://github.com/dlang/phobos/blob/v2.071.2/std/range/package.d#L4722) and data coming from the network? And how would you get the number of remaining elements in the range?

The argument is not that the length of a range should always 
evaluate to the length of some container it enumerates, but that 
the length of the range should not change based on your position 
in the range. In phobos, popFront effectively reduces the length 
of the range by one, and offsets all further calls to opIndex and 
opSlice. I expect length, opIndex, and opSlice to behave the same 
whether I just built the range, am halfway through the range, or 
have fully consumed the range.

>> Phobos' range facilities vomit when you try to deal with 
>> static arrays, and they have to be coerced into dynamic arrays 
>> before they can be dealt with. This is silly.
>
> What's the problem here? Just slice them using the [] syntax 
> (i.e. the analog of asrange in your library).

As Jonathan pointed out there's a lot of muddying of how dynamic 
arrays are only sorta-kinda genuine arrays. But when using them 
one should be able to rely on dynamic and static arrays have the 
same interface, except that one can be resized and appended to 
and another cannot. Having to slice static arrays to pass them 
into phobos' HOFs is icky and completely unnecessary. And though 
you might say that you could just append `[]` to every array you 
pass in, but while it will work for static and dynamic arrays it 
won't work for everything that can be passed to a function 
accepting a range.

Consistency of syntax for performing the same operation upon many 
different types is not to be undervalued.



More information about the Digitalmars-d mailing list