constness for arrays
Andrew Fedoniouk
news at terrainformatica.com
Tue Jul 18 17:42:02 PDT 2006
Dynamic constness versus static (compile time) constness is not new.
For example in Ruby you can dynamicly declare object/array readonly and
its runtime will control all modifications and note - in full as Ruby's
sandbox
(as any other VM based runtime) has all facilities to fully control
immutability of such objects.
In case of runtimes like D (natively compileable) such control is not an
option.
I beleive that proposed runtime flag a) is not a constness in any sense
b) does not solve compile verification of readonlyness and
c) can be implemented now by defining:
struct vector
{
bool readonly;
T* data;
uint length;
}
Declarative contness prevents data misuse at compile time
when runtime constness moves problem into execution time
when is a) too late to do anything and b) expensive.
I would mention old idea again - real solution would be in creating of
mechanism of disabling exiting or creating new opertaions
for intrinsic types.
For example string definition might look like as:
typedef string char[]
{
disable opAssign;
....
char[] tolower() { ..... }
}
In any case such mechanism a) is more universal than const in C++
b) allows to create flexible type systems and finally
c) this will also legalize situation with
"external methods" D has now for array types.
The later one alone is a good enough motivation to do so
as current situation with "external methods" looks like as
just a bug of design or compiler to be honest.
I am yet silent that it will make D's type system unique
in this respect among other languages.
Andrew Fedoniouk.
http://terrainformatica.com
More information about the Digitalmars-d
mailing list