www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - tail const ?

reply "sclytrack" <sclytrack fake.com> writes:
As Stewart Gordon mentioned before (2012)

What about adding tailConst to the D programming language?


tailConst MyStruct a;

All fields of the MyStruct would be tailConst and ...



tailConst MyClass b;

would make the the pointer mutable but all the fields of b const.



...tailImmutable, ...
Oct 29 2014
next sibling parent reply "Simon A" <simon_a example.com> writes:
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.)
Oct 30 2014
next sibling parent "sclytrack" <fake hotmail.com> writes:
On Thursday, 30 October 2014 at 10:28:42 UTC, Simon A wrote:
 I don't know about syntax, but D sure needs first-class support 
 of tail immutability.
struct A { }
Oct 30 2014
prev sibling parent "sclytrack" <sclytrack fake.com> writes:
On Thursday, 30 October 2014 at 10:28:42 UTC, Simon A wrote:
 I don't know about syntax, but D sure needs first-class support 
 of tail immutability.
struct A { float * a; void foo() tailconst { writeln(typeof(a).stringof); //outputs "const(float) *" } } class B { float * a; void foo() tailconst { writeln(typeof(this).stringof); //outputs "tailconst(B)" writeln(typeof(a).stringof); //outputs "const float *" } } tailconst for struct and classes. How about this? *sorry about the previous almost empty post.
Oct 30 2014
prev sibling parent "Dicebot" <public dicebot.lv> writes:

Nov 01 2014