number ranges

Salih Dincer salihdb at hotmail.com
Thu Jan 20 05:24:36 UTC 2022


Hi,

It looks so delicious. 😀 Thank you.

On Wednesday, 19 January 2022 at 18:59:10 UTC, Ali Çehreli wrote:
>
> And adding length() was easy as well.
>
> Finally, I have provided property functions instead of allowing 
> direct access to members.
>
It doesn't matter as we can't use a 3rd parameter.  But it 
doesn't work for any of these types: real, float, double.

My solution:
```d
   size_t length() inout {
     //return last_ - first_ + 1 - empty_;/*
     auto len = 1 + last_ - first_;
     return cast(size_t)len;//*/
   }
```
But it only works on integers. In this case, we have two options! 
The first is to require the use of integers, other 3 parameter 
usage:
```d
// ...
   size_t length() inout {
     auto len = 1 + (last - front) / step;
     return cast(size_t)len;
   }
} unittest {
   enum { ira = 0.1,
          irb = 2.09,
          irc = 0.11
        }
   auto test = inclusiveRange(ira, irb, irc);
   assert(test.count == 19);

   auto arr = iota(ira, irb, irc).array;
   assert(test.length == arr.length);
}
```

Salih


More information about the Digitalmars-d-learn mailing list