alias this versus opDot

Jonathan M Davis jmdavisProg at gmx.com
Sun Nov 11 14:22:56 PST 2012


On Sunday, November 11, 2012 14:09:51 Ali Çehreli wrote:
> On 11/11/2012 12:38 PM, Namespace wrote:
> > I have this code: http://dpaste.dzfl.pl/131ca7e9
> > Why I get these error messages if I try to use a @property method with
> > alias this? And why it works fine if I use opDot?
> 
> For what it's worth, this combination compiles:
> 
> /* returns immutable(T) but 'this' is inout */
>      @property
>          immutable(T) get() inout pure nothrow {
>          return this._val;
>      }
> 
>      alias get this;
> 
> /* ... */
> 
> void foo(immutable A a)
> {}
> 
> void main() {
>      Unique!(A) uni = new A();
>      foo(uni);
> }

Wow. That looks like a nasty bug. get becomes callable (and therefore works 
with alias this), because it's then inout instead of immutable, but it's then 
trying to convert _val - which is an A - to an immutable A in the return 
value. That conversion is illegal and should give an error. I could see it not 
giving an error when get is declared, because it _would_ work if the this 
reference were immutable, but you're actually using it when calling foo, and 
it should _definitely_ error out then.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list