opImplicitCast/opImplicitCastFrom

bearophile bearophileHUGS at lycos.com
Thu Oct 30 05:57:11 PDT 2008


A possible Pascal-inspired syntax to specify the bounds of all integral values:

typedef int : 1 .. 7 TyDice1; // int in [1, 6]
typedef ubyte : 1 .. 7 TyDice2; // ubyte in [1, 6]
typedef ubyte : 1 .. 300 TyWarriors; // compilation error
int : 1 .. 1000 i; // int in [0, 999]
ubyte ub; // the same as  ubyte : 0 .. 256 ub;
char : 'a' .. 'z'+1 c; // char in [a, z]

Possibile alternative syntax:

typedef int TyDice1 : 1 .. 7; // int in [1, 6]
typedef ubyte TyDice2 : 1 .. 7; // ubyte in [1, 6]
typedef ubyte TyWarriors : 1 .. 300; // compilation error
int i : 1 .. 1000; // int in [0, 999]
ubyte ub; // the same as  ubyte ub : 0 .. 256;
char c: 'a' .. 'z'+1; // char in [a, z]

Plus some syntax to locally enable/disable the bound checks (-release disables them globally):

unsafe(integral, bounds) {
    // here both array bounds and integral bounds aren't checked
    ...
}

Bye,
bearophile



More information about the Digitalmars-d mailing list