Thread overview
alias this versus opDot
Nov 11, 2012
Namespace
Nov 11, 2012
Rob T
Nov 11, 2012
Namespace
Nov 11, 2012
Jonathan M Davis
Nov 11, 2012
Ali Çehreli
Nov 11, 2012
Jonathan M Davis
Nov 11, 2012
Ali Çehreli
November 11, 2012
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?
November 11, 2012
On Sunday, 11 November 2012 at 20:38:21 UTC, 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?

Where is opDot described in the D spec?

.. OK I found this thread, it's been depreciated
http://www.digitalmars.com/d/archives/digitalmars/D/learn/opDot_alias_this_37533.html

No answer on opStar though.

In any event, the problem you are experiencing could be related to the broken template system. We've been having a trouble defining templates of structures that are perfectly legit and work fine when the type is specified in non-templated form.

Try the same code in non-template form to see if it works or not.

--rt
November 11, 2012
I think that the problem is the immutable modifier. Without immutable(T) and immutable as method modifier it works fine too.
My question is: why?
November 11, 2012
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);
}

Ali
November 11, 2012
On Sunday, November 11, 2012 22:30:06 Namespace wrote:
> I think that the problem is the immutable modifier. Without immutable(T) and immutable as method modifier it works fine too. My question is: why?

It's because _val isn't immutable. It's an A, and you can't implicitly convert A to immutable A. So, neither opDot or get is useable with Unique!A. It would have to be immutable(Unique!(immutable A)) for it to work (since Unique must be immutable for them to be callable, and _val must be immutable A for it to be able to be returned from them.

The reason that the get is blowing up is that you're using it with alias this, which then makes it so that you're trying to use get, which then fails, because the this reference isn't immutable, whereas you're not trying to use opDot, so the compiler doesn't complain.

- Jonathan M Davis
November 11, 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
November 11, 2012
On 11/11/2012 02:22 PM, Jonathan M Davis wrote:

> Wow. That looks like a nasty bug.

Posted:

  http://d.puremagic.com/issues/show_bug.cgi?id=8998

Ali