this() const

sclytrack sclytrack at hotmail.com
Mon Apr 16 07:39:58 PDT 2012


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 at 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.







More information about the Digitalmars-d-learn mailing list