constness for arrays

Dave Dave_member at pathlink.com
Tue Jul 18 19:53:33 PDT 2006


Andrew Fedoniouk wrote:
> 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
> 
> 

What do you mean by external methods?

This?

import std.stdio;
void main()
{
     char[] str = "abc";
     writefln(str.ucase()); // "ABC"
}
char[] ucase(char[] str)
{
     foreach(inout char c; str) if(c >= 'a' && c <= 'z') c += 'A' - 'a';
     return str;
}

If so, that's not a bug, it's intentional. Line 4141 of expression.c.

- Dave



More information about the Digitalmars-d mailing list