April 17, 2004 Possible solution for D's missing const | ||||
---|---|---|---|---|
| ||||
//module 1
template constArray(T) //This would be in another module
{
struct Array
{
private T [] array;
static Array opCall(T [] array)
{
Array t;
t.array = array;
return t;
}
T opIndex(int i) { return array[i]; }
int length() { return array.length; }
}
}
alias constArray!(int).Array constA;
//module 2
void test(constA array)
{
int x = array[0];
//You just can't change the array variable (well at least not without force).
}
void main()
{
int [] a;
a ~= 10;
a ~= 30;
test(constA(a)); //Ok there's a small bit of extra typing -> if D only had automatic boxing....
}
--
-Anderson: http://badmama.com.au/~anderson/
|
Copyright © 1999-2021 by the D Language Foundation