Re: Subrange type

2020-01-28 Thread Ali Çehreli via Digitalmars-d-learn

On 1/28/20 1:55 AM, Herbert wrote:

> wow, pragma,

pragma was to indicate the type of elements at compile time.

> import ...

Yes, iota is a Phobos feature, part of a module.

> only to declare a subrange type, something so simple and natural

Some languages think so.

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

Sorry for jumping in the conversation like that but no, it's not a 
subrange type.


> It's a pitty.

My apologies...

Ali



Re: Subrange type

2020-01-28 Thread Herbert via Digitalmars-d-learn

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.


Re: Subrange type

2020-01-27 Thread Ali Çehreli via Digitalmars-d-learn

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:
How can I create a subrange type, for example ushort DiceValue {1 ... 
6}?


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


Re: Subrange type

2020-01-27 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Jan 27, 2020 at 09:15:58PM +, Herbert via Digitalmars-d-learn wrote:
[...]
> How can I have a function parameter with this type (DiceValue)?

Just take an integer type and add a contract that enforces range. For
example:

auto myFunc(int diceValue)
in (diceValue >= 1 && diceValue <= 6)
{
// do stuff here
}


T

-- 
Which is worse: ignorance or apathy? Who knows? Who cares? -- Erich Schubert


Re: Subrange type

2020-01-27 Thread Herbert via Digitalmars-d-learn
On Monday, 27 January 2020 at 20:15:33 UTC, Steven Schveighoffer 
wrote:

On 1/27/20 3:06 PM, Herbert wrote:
How can I create a subrange type, for example ushort DiceValue 
{1 ... 6}?


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)?



Re: Subrange type

2020-01-27 Thread Paul Backus via Digitalmars-d-learn

On Monday, 27 January 2020 at 20:06:14 UTC, Herbert wrote:
How can I create a subrange type, for example ushort DiceValue 
{1 .. 6}?


Probably the closest you can get is a struct with an invariant:

import std.traits: isOrderingComparable;

struct Subrange(T, T min, T max)
if (isOrderingComparable!T)
{
private T value_ = min;
invariant(min <= value && value <= max);

this(T t) { value_ = t; }

T value() { return value_; }
alias value this;

ref Subrage opAssign(T t) { value_ = t; return this; }
}


Re: Subrange type

2020-01-27 Thread Steven Schveighoffer via Digitalmars-d-learn

On 1/27/20 3:06 PM, Herbert wrote:

How can I create a subrange type, for example ushort DiceValue {1 ... 6}?


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


Subrange type

2020-01-27 Thread Herbert via Digitalmars-d-learn
How can I create a subrange type, for example ushort DiceValue {1 
.. 6}?