constness for arrays

Chad J gamerChad at _spamIsBad_gmail.com
Tue Jul 18 18:51:53 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
> 

I like that typedef.  Should be templatable though...

typedef(T) array T[]
{
     ...
}

Or some such.  In an earlier post ("Module level operator overloading" 
at http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/39504) I was 
hoping for external functions as operator overloads and IFTI to help 
with things like array operations.  I just didn't know about external 
functions at the time.  But if this is supposed to replace external 
functions, how would I do the array op overloads that external functions 
would help me with?  Would be unfortunate to write something like this...

typedef(T) T[] T[] // mmm what would this do
{
   void opAdd(T[] array1, T[] array2)
   {
     etc...
   }

   ...
}



More information about the Digitalmars-d mailing list