Proposed Changes to the Range API for Phobos v3

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri May 17 04:48:24 UTC 2024


On Thursday, May 16, 2024 7:30:06 PM MDT monkyyy via Digitalmars-d wrote:
> if I define a complex opDollar, I can easily imagine it being
> seen as the "indexing" type, when in fact im using it it to slice
> on a bidirectional datastruct that maybe knows how to handle $/2
>
> ```d
> struct mydata{
>    auto opDollar(){
>      struct dollar{
>        opBinary!"-"
>        opBinary!"/"
> ```
>
> if the range api makes an assumption that
> `range.opSlice!(typeof(range.opDollar),typeof(range.opDollar))`
> this may break unnecessarily; where opDollar is used in opslicing
> matters, also like add $/2 to the wishlist

Essentially, that requires that opDollar define the common arithmetic
operations (except maybe multiplication). I'll have to think about the edge
cases to decide how reasonable that is (though the fact that you can often
use a single opBinary for all of the arithmetic operations helps). Whatever
the operations are, _every_ finite range that defines indexing or slicing
would then have to define all of them in order for generic code to be able
to rely on them.

Fortunately, in most cases, opDollar should just be an alias to length, so
it may be reasonable, much as it could be annoying for the corner cases.

> >>is [min(i,$-1)] supported?
> >
> >_____
>
> while I know such slicing probably isnt on your rader for being
> to magic I unironically like how well it works for the base slice

I was not aware that $ could be used anywhere except directly within the
indexing or slicing expression (or if I knew, I forgot). I guess that the
compiler lets it be used anywhere between the brackets, and since the result
is just size_t with arrays, it works just fine with min. A user-defined type
would have to implement opCmp for that to work. So having min work would
mean requiring opCmp on top of the arithmetic operations. That might be
reasonable, but it definitely adds to the complexity of implementing
opDollar for types that can't just alias length, though realistically, there
aren't going to be many finite ranges which implement opDollar another way.

So, I'll have to think about it further, but whatever the operations are
that ranges will be able to use with $ in generic code will be required by
the API and documented accordingly (if not, there would be no way to rely on
being able to use them in generic code).

- Jonathan M Davis





More information about the Digitalmars-d mailing list