On Wednesday, 16 October 2013 at 17:16:39 UTC, Dicebot wrote:

It works as it should. Make a mutable copy of t2 and pass it. Or make foo() accept const. I can't imagine a single legitimate use case for destroying type system in a way you want.

How do you propose to make a mutable copy *generically*?

I think a legitimate use would be the following. In the construction of S I want to use a T to construct an R. `this(...) immutable {}` prevents initialization of R in this case. Maybe immutable adorning `this()` should mean it is immutable upon the end of the construction, giving the initialization a chance to actually initialize. In the example below a better approach would be to have:

R createRFromT(ref const(T) t) pure {...}

but with what I'm using from phobos that is not possible.

------------------------------------------------

import std.conv;
import std.stdio;

struct R {
  int[] i;
  string[string] ss;
}

struct T {
  int[] i;
  string[string] ss;
}

struct S {
  R r;
  this(ref const(T) t) immutable {
    createRFromT(t, r);
  }
}

void createRFromT(ref const(T) t, ref R r) {
  //...
}

void main() {
  T t1;
  auto t2 = immutable T();
  auto s = immutable S(t2);
}

Reply via email to