Ranges with length that cannot be named

Dominikus Dittes Scherkl dominikus.scherkl at continental-corporation.com
Thu Sep 19 10:45:50 UTC 2019


On Wednesday, 18 September 2019 at 19:10:43 UTC, berni wrote:
> We are used to divide ranges in two: Ranges with length and 
> infinite ranges. But there is something in between. Some ranges 
> have a length, that does not fit in an (u)long and therefore 
> cannot be named in D. The same might be the case, when the 
> calculation of the length is somehow problematic.
[...]
> Some ideas came to my mind:
>
> a) return 0, but empty()==false
> b) throw an Exception
> c) return ulong.max (or size_t.max or long.max or whatever type 
> is used for the length of ranges)
> d) using BigInt
>

I would go with (c) [better: return T.max, with T the type of 
length]
because why would you ask for the length? Because you want to 
ensure that you can get enough from the stream e.g. to fill some 
buffer or to check if it would fit in some data structure. And to 
both the answer T.max would be fine: you can always get enough 
and it won't fit in whatever you have reserved.

(d) would only be useful if the target is also a range with 
voldemort-length, which would very seldom the case, and even 
there T.max is a good starting point.


More information about the Digitalmars-d mailing list