tail const ?

Simon A via Digitalmars-d digitalmars-d at puremagic.com
Thu Oct 30 03:28:40 PDT 2014


I don't know about syntax, but D sure needs first-class support 
of tail immutability.

Take this code for example:

---

immutable class C
{
	void foo()
	{
		//
	}
}

void main()
{
	auto c = new C();
	c.foo();
}

---

Compile it and get

Error: immutable method C.foo is not callable using a mutable 
object

Why?  Because foo() is immutable and demands an immutable this 
reference.  The C instance c is only tail immutable, which 
doesn't really count for anything.  (So, "new C()" instead of 
"new immutable(C)()" is legal but pretty much unusable, it seems.)

But why should *any* function require an immutable reference, as 
opposed to a tail immutable reference?  (Similarly for shared vs 
tail shared.)


More information about the Digitalmars-d mailing list