Subrange type

Herbert ht at softdesign.de
Tue Jan 28 09:55:42 UTC 2020


On Monday, 27 January 2020 at 22:05:57 UTC, Ali Çehreli wrote:
> On 1/27/20 1:15 PM, Herbert wrote:
>> On Monday, 27 January 2020 at 20:15:33 UTC, Steven 
>> Schveighoffer wrote:
>>> On 1/27/20 3:06 PM, Herbert wrote:
>>>> [...]
>>>
>>> D doesn't have a "Range" type like this. But you can use 
>>> ranges of different types by typing the literals. Note that D 
>>> numeric ranges are always exclusive at the upper end.
>>>
>>> e.g.:
>>>
>>> ushort(1) .. ushort(6+1)
>>>
>>> -Steve
>> 
>> Thank you Steven!
>> 
>> How can I have a function parameter with this type (DiceValue)?
>> 
>
> There is also iota() than generates a range:
>
> import std.stdio;
> import std.range;
>
> void foo(R)(R range) {
>   pragma(msg, "Element type: ", ElementType!R);
>
>   writefln!"Using as a range:\n%-(%s\n%)"(range);
>
>   writeln("Using in a foreach loop:");
>
>   foreach (element; range) {
>     writeln(element);
>   }
> }
>
> void main() {
>   auto range = iota(ushort(1), ushort(7));
>   foo(range);
> }
>
> The output:
>
> Using as a range:
> 1
> 2
> 3
> 4
> 5
> 6
> Using in a foreach loop:
> 1
> 2
> 3
> 4
> 5
> 6
>
> Ali

wow, pragma, import ...
only to declare a subrange type, something so simple and natural

I could use it in many functions as a parameter type with one 
single simple declaration.

It's a pitty.


More information about the Digitalmars-d-learn mailing list