Ada-Style Modulo Integer Types

Timon Gehr via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 22 13:05:49 PDT 2016


On 22.04.2016 21:52, Nordlöw wrote:
> On Friday, 22 April 2016 at 17:37:44 UTC, Nordlöw wrote:
>> Have anybody implement Ada-style modulo types
>>
>> https://en.wikibooks.org/wiki/Ada_Programming/Types/mod
>
> Here's my first try
>
> https://github.com/nordlow/phobos-next/blob/master/src/modulo.d
>
> Is there a way to use
>
>     alias _value this;
>
> except mutation of _value which should check that _value is within value
> range (0 .. m-1)?

Why do you want to perform a range check? Shouldn't it just take the 
appropriate remainder?

Anyway, the following will do what you request:

struct Mod(int M){
     private int x;
     @property int prop(int x)in{assert(0<=x&&x<M);}body{
         return this.x=x;
     }
     @property int prop(){ return x; }
     alias prop this;
}



More information about the Digitalmars-d-learn mailing list