On 12/13/2016 01:36 PM, Ali wrote:

Now about that second part of my problem ....

I'm not entirely sure whether this should work but I think the problem is with mutating the 'frequencies' member of an immutable element of 'rooms'. The error message means that those non-const expressions cannot be shared by a member of an immutable AA.

I moved the frequencies to 'data' and hte following worked. Used 'shared static this' so that the data is populated per application instead of per thread. Additionally, the 'd.room.name.writeln' expression works just fine with DMD64 D Compiler v2.072.1.

import std.algorithm: splitter, map;
import std.array: array;
import std.typecons: Tuple;
import std.stdio: writeln;

static immutable Room[] rooms = import("data.txt").splitter.map!parse.array;

struct Room {
    string name;
}

static Tuple!(const(Room*), "room", int[char], "frequencies")[rooms.length] data;

shared static this() {
    foreach (i, ref room; rooms) {
        data[i].room = &room;
        foreach (letter; room.name) {
            data[i].frequencies[letter]++;
        }
    }
}

Room parse(string line) pure {
    immutable name = line;
    return Room(name);
}

void main() {
    foreach (d; data) {
        d.room.name.writeln;
        d.frequencies.writeln;
    }
}

Ali

  • ... Ali via Digitalmars-d-learn
    • ... drug007 via Digitalmars-d-learn
      • ... Ali via Digitalmars-d-learn
        • ... bauss (wtf happend to my name took some old cached title LOL??) via Digitalmars-d-learn
    • ... Ali Çehreli via Digitalmars-d-learn
      • ... Ali via Digitalmars-d-learn
        • ... Ali Çehreli via Digitalmars-d-learn
          • ... Ali via Digitalmars-d-learn
            • ... Ali Çehreli via Digitalmars-d-learn
              • ... Stefan Koch via Digitalmars-d-learn
                • ... Ali Çehreli via Digitalmars-d-learn
                • ... Ali via Digitalmars-d-learn

Reply via email to