www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - this() const

reply sclytrack <sclytrack hotmail.com> writes:
	this( const size_t step) const
	{
		this.step = step;
	}


Error: cannot modify const/immutable/inout expression this.step


Is this the expected behavior? Thanks.
Apr 15 2012
parent reply Trass3r <un known.com> writes:
Am 15.04.2012, 21:20 Uhr, schrieb sclytrack <sclytrack hotmail.com>:

 	this( const size_t step) const
 	{
 		this.step = step;
 	}


 Error: cannot modify const/immutable/inout expression this.step


 Is this the expected behavior? Thanks.
Yep.
Apr 15 2012
parent reply Simon <s.d.hammett gmail.com> writes:
On 15/04/2012 21:07, Trass3r wrote:
 Am 15.04.2012, 21:20 Uhr, schrieb sclytrack <sclytrack hotmail.com>:

 this( const size_t step) const
 {
 this.step = step;
 }


 Error: cannot modify const/immutable/inout expression this.step


 Is this the expected behavior? Thanks.
Yep.
No it's not: import std.stdio; struct foo { size_t _a; this(const size_t b) const { this._a = b; } } class bar { size_t _a; this(const size_t b) const { this._a = b; } } void main() { const a = new foo(42); const b = new bar(42); writefln("a._a: %d\n", a._a); writefln("b._a: %d\n", b._a); } works as expected. dmd v2.058 -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Apr 15 2012
parent sclytrack <sclytrack hotmail.com> writes:
On 04/15/2012 11:04 PM, Simon wrote:
 On 15/04/2012 21:07, Trass3r wrote:
 Am 15.04.2012, 21:20 Uhr, schrieb sclytrack <sclytrack hotmail.com>:

 this( const size_t step) const
 {
 this.step = step;
 }


 Error: cannot modify const/immutable/inout expression this.step


 Is this the expected behavior? Thanks.
Yep.
No it's not: import std.stdio; struct foo { size_t _a; this(const size_t b) const { this._a = b; } } class bar { size_t _a; this(const size_t b) const { this._a = b; } } void main() { const a = new foo(42); const b = new bar(42); writefln("a._a: %d\n", a._a); writefln("b._a: %d\n", b._a); } works as expected. dmd v2.058
Thanks. That was helpful (no irony). Compiler message was a bit fuzzy. this() const // Here the const modifies the return type // While in normal methods it shouldn't. These "constness baking" seems only to be allowed in constructors.
Apr 16 2012