Memory allocation purity

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Fri May 16 02:22:10 PDT 2014


Walter Bright:

> Yes, the VRP has been a nice win for D.

There are ways to improve it in very useful ways, that increase 
static safety and cut down the number of useless casts required. 
Some currently refused examples:

---------------

int[100] array;
void main() {
     foreach (immutable idx, const x; array)
         ubyte y = idx;
}

---------------

void foo(immutable uint x)
in {
     assert(x <= ubyte.max);
} body {
     ubyte y = x;
}
void main() {}


https://issues.dlang.org/show_bug.cgi?id=10751



More on contracts:

uint foo()
in {
} out(result) {
     assert(result < 100);
} body {
     return 20;
}
void bar(immutable ubyte x) {}
void main() {
     bar(foo());
}

---------------

D immutability is under-utilized, it avoids the need for flow 
analysis:


void main(in string[] args) {
     immutable ushort x = args.length % 5;
     immutable ubyte y = x;
}



uint x;
void main() {
     immutable uint y = x;
     if (y <= ubyte.max) {
         ubyte z = y;
     }
}



uint x;
void main() {
     immutable uint y = x;
     assert(y <= ubyte.max);
     ubyte z = y;
}


https://issues.dlang.org/show_bug.cgi?id=10594

---------------

More:
https://issues.dlang.org/show_bug.cgi?id=10615
https://issues.dlang.org/show_bug.cgi?id=12753

Bye,
bearophile


More information about the Digitalmars-d mailing list