Initialization of mutable struct by immutable struct

Uranuz neuranuz at gmail.com
Wed Nov 27 22:05:02 PST 2013


In addition to my letter I wrote some test code following the 
message. I found out that I can use copy constructor signature: 
this(immutable(this)), to get a mutable copy of immutable struct. 
But this is not working with this(const(this)). My thoughts were 
that if I would define some method parameter as const so it will 
accept both mutable and immutable arguments. Is there some 
exceptional situation that it's not true for copy constructors or 
is it a bug or is it my misunderstanding of this question?

//-------------------
import std.stdio;

struct A
{
	this(string some) immutable
	{  _some = some; }
	
	//this(immutable(A) a)
	//{   _some = a._some; }
	
	this(immutable(this)) {}  //This is working with immutable
	
	//this(const(this)) {}  //But this is not working with immutable
	
	void printSome() { writeln(_some); }
	
	private string _some;
}

class B
{  this(A a)
{  _a = a; }
  private A _a;
  void printA() { _a.printSome(); }
}

immutable(A) globalA = immutable(A)("Global");

void main()
{
	//B b = new B( A(globalA) );
	B b = new B( globalA ); //immutable globalA copying into mutable
	b.printA();
}
//--------------

Also available at: http://dpaste.dzfl.pl/3c89185e


More information about the Digitalmars-d-learn mailing list