internal compiler error with immutable

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Feb 8 01:42:13 PST 2015


On 02/07/2015 06:35 AM, Danny wrote:

 > What I'm trying to do is save space by putting often-used Attrs into an
 > array and just compressing references to those into 1 Byte in the other
 > data structures where it's used (these other data structures are not
 > thread-local).

The following is based on my understanding of that description:

import std.conv;
import std.concurrency;
import std.stdio;

class C
{
     int value;

     // Constructor for immutable(C) objects
     this (int value) immutable
     {
         this.value = value;
     }
}

// Stores commonly used C objects and allows access to them by
// ubyte indexes
class CommonlyUsedCs
{
     immutable(C)[] cs;

     // This add() works on 'shared' CommonlyUsedCs objects
     ubyte add(immutable(C) c) shared
     {
         ubyte index;

         synchronized {
             index = cs.length.to!ubyte;
             cs ~= c;
         }

         return index;
     }

     immutable(C) get(ubyte index) const shared
     {
         return cs[index];
     }
}

void main()
{
     auto cucs = new shared(CommonlyUsedCs)();

     // Populate the Cs
     foreach (ubyte i; 0 .. 10) {
         if (i % 2) {
             cucs.add(new immutable(C)(i));
         }
     }

     // Start a thread and tell it what index its C is at
     spawn(&user, cucs, ubyte(3));
}

void user(shared(CommonlyUsedCs) cucs, ubyte index)
{
     writefln("My C has value %s", cucs.get(index).value);
}

Ali



More information about the Digitalmars-d-learn mailing list