Inability to dup/~ for const arrays of class objects

Ali Çehreli acehreli at yahoo.com
Wed May 29 09:17:27 PDT 2013


On 05/28/2013 08:43 PM, Peter Williams wrote:

 > One place I'm using it is for managing symbols (look ahead sets, etc) in
 > a parser generator.  The symbols' permanent home is in an associative
 > array indexed by their name (which is also included in the symbol
 > object) but they may belong to many look ahead sets.

Have you considered Rebindable?

import std.typecons;

class C
{
     void mutate() {}
     void readOnly() const {}
}

void main()
{
     // Here is the actual data:
     C[string] mutableData;
     mutableData["one"] = new C();
     mutableData["two"] = new C();

     // Mutable; good...
     mutableData["one"].mutate();

     alias ConstRef = Rebindable!(const C);

     // Here is a const view of mutable data
     ConstRef[] set;
     set ~= ConstRef(mutableData["one"]);
     set ~= ConstRef(mutableData["two"]);

     set[0].readOnly();
     // set[0].mutate();  <-- compilation error; good...
}

Ali



More information about the Digitalmars-d mailing list