User:Schuetzm/isolated

From D Wiki
Revision as of 15:27, 12 April 2015 by Schuetzm (talk | contribs) (Created page with "== Isolated == Currently, this works: <source lang="D"> void main() { immutable int* p = new int; } </source> This doesn't: <source lang="D"> struct S { int* p; }...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Isolated

Currently, this works:

void main() {
    immutable int* p = new int;
}

This doesn't:

struct S {
    int* p;
}

void main() {
    auto tmp = new S;
    tmp.p = new int;
    immutable S* s = tmp;    // Error: cannot implicitly convert expression (tmp) of type S* to immutable(S*)
}

But if we wrap the construction in a pure function, it does work:

struct S {
    int* p;
}

S* makeS() pure {
    auto tmp = new S;
    tmp.p = new int;
    return tmp;
}

void main() {
    immutable S* s = makeS();
}

By utilizing deadalnix' "isolated" idea, we can make the second example work, too. This will be useful for incremental construction of complex immutable data structures, as well as data structures that should be transferred to another thread.

TODO: specify the rules (islands concept)