www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias this versus opDot

reply "Namespace" <rswhite4 googlemail.com> writes:
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?
Nov 11 2012
next sibling parent "Rob T" <rob ucora.com> writes:
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
Nov 11 2012
prev sibling next sibling parent "Namespace" <rswhite4 googlemail.com> writes:
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?
Nov 11 2012
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Nov 11 2012
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Nov 11 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Nov 11 2012
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, November 11, 2012 14:09:51 Ali =C3=87ehreli 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 w=


 alias this? And why it works fine if I use opDot?

For what it's worth, this combination compiles: =20 /* returns immutable(T) but 'this' is inout */ property immutable(T) get() inout pure nothrow { return this._val; } =20 alias get this; =20 /* ... */ =20 void foo(immutable A a) {} =20 void main() { Unique!(A) uni =3D new A(); foo(uni); }

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