www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - restrict alias this?

reply "Namespace" <rswhite4 googlemail.com> writes:
Based on my previous thread 
(http://forum.dlang.org/thread/rpcqefmyqigftxsgsqui forum.dlang.org), 
I got the question whether it is possible to restrict "alias 
this"?
Similar to templates function, e.g. "void foo(T)(T value) if 
(isNumeric!(T) ...", but it should be limited here so that it 
does not interact with the cast or something other.
As far as I know, "alias this" is an implicit cast. And just like 
an explicit cast (opCast) it should be possible to restrict it.

Otherwise, I have unfortunately run out of ideas how I can allow 
an implicit conversion.
Apr 29 2012
next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Sun, 29 Apr 2012 10:14:09 +0200, Namespace <rswhite4 googlemail.com>  
wrote:

 Based on my previous thread  
 (http://forum.dlang.org/thread/rpcqefmyqigftxsgsqui forum.dlang.org), I  
 got the question whether it is possible to restrict "alias this"?
 Similar to templates function, e.g. "void foo(T)(T value) if  
 (isNumeric!(T) ...", but it should be limited here so that it does not  
 interact with the cast or something other.
 As far as I know, "alias this" is an implicit cast. And just like an  
 explicit cast (opCast) it should be possible to restrict it.

 Otherwise, I have unfortunately run out of ideas how I can allow an  
 implicit conversion.

What do you mean restrict it?
Apr 29 2012
prev sibling next sibling parent "Namespace" <rswhite4 googlemail.com> writes:
On Sunday, 29 April 2012 at 11:24:00 UTC, Simen Kjaeraas wrote:
 On Sun, 29 Apr 2012 10:14:09 +0200, Namespace 
 <rswhite4 googlemail.com> wrote:

 Based on my previous thread 
 (http://forum.dlang.org/thread/rpcqefmyqigftxsgsqui forum.dlang.org), 
 I got the question whether it is possible to restrict "alias 
 this"?
 Similar to templates function, e.g. "void foo(T)(T value) if 
 (isNumeric!(T) ...", but it should be limited here so that it 
 does not interact with the cast or something other.
 As far as I know, "alias this" is an implicit cast. And just 
 like an explicit cast (opCast) it should be possible to 
 restrict it.

 Otherwise, I have unfortunately run out of ideas how I can 
 allow an implicit conversion.

What do you mean restrict it?

I mean that you can limit it similar as you can with templates. In my code i'm using "alias this" to enable an implicit conversion from any class, e.g. Foo, to Ref!Foo. Ref is an struct which checks if the value of Foo is null. If it's null a error message will let you know, that you have a problem with one of your objects (i hate the access violation message, that's the original reason for that). But if i cast with a class, that implements the implicit conversion, as in the following: [code] class A { mixin TRef!(A); } class B : A { } A a1 = new B(); B b1 = cast(B) a1.access; // <- problem [/code] the cast will implicit convert with "alias this" to Ref!B. And that isn't what i want and neither what should happen. In this case a limit to "alias this" would be great. A limitation, that "alias this" must not convert the cast to Ref!B.
Apr 29 2012
prev sibling next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Sun, 29 Apr 2012 14:28:05 +0200, Namespace <rswhite4 googlemail.com>  
wrote:

 On Sunday, 29 April 2012 at 11:24:00 UTC, Simen Kjaeraas wrote:
 On Sun, 29 Apr 2012 10:14:09 +0200, Namespace <rswhite4 googlemail.com>  
 wrote:

 Based on my previous thread  
 (http://forum.dlang.org/thread/rpcqefmyqigftxsgsqui forum.dlang.org),  
 I got the question whether it is possible to restrict "alias this"?
 Similar to templates function, e.g. "void foo(T)(T value) if  
 (isNumeric!(T) ...", but it should be limited here so that it does not  
 interact with the cast or something other.
 As far as I know, "alias this" is an implicit cast. And just like an  
 explicit cast (opCast) it should be possible to restrict it.

 Otherwise, I have unfortunately run out of ideas how I can allow an  
 implicit conversion.

What do you mean restrict it?

I mean that you can limit it similar as you can with templates. In my code i'm using "alias this" to enable an implicit conversion from any class, e.g. Foo, to Ref!Foo. Ref is an struct which checks if the value of Foo is null. If it's null a error message will let you know, that you have a problem with one of your objects (i hate the access violation message, that's the original reason for that). But if i cast with a class, that implements the implicit conversion, as in the following: [code] class A { mixin TRef!(A); } class B : A { } A a1 = new B(); B b1 = cast(B) a1.access; // <- problem [/code] the cast will implicit convert with "alias this" to Ref!B. And that isn't what i want and neither what should happen. In this case a limit to "alias this" would be great. A limitation, that "alias this" must not convert the cast to Ref!B.

But that's not what happens. a1's compile-time type is A, so the compiler looks up 'access' in A, but finds nothing. So it tries the alias this, gets a Ref!A, and calls access on that. At this point, T is A, so access returns an A. Then, the compiler tries to convert that A to a B, and gets confused. This is absolutely a bug. In fact, it is the bug Jesse Phillips reported[1]. [1]: http://d.puremagic.com/issues/show_bug.cgi?id=8001
Apr 29 2012
prev sibling parent "Namespace" <rswhite4 googlemail.com> writes:
On Sunday, 29 April 2012 at 17:29:03 UTC, Simen Kjaeraas wrote:
 On Sun, 29 Apr 2012 14:28:05 +0200, Namespace 
 <rswhite4 googlemail.com> wrote:

 On Sunday, 29 April 2012 at 11:24:00 UTC, Simen Kjaeraas wrote:
 On Sun, 29 Apr 2012 10:14:09 +0200, Namespace 
 <rswhite4 googlemail.com> wrote:

 Based on my previous thread 
 (http://forum.dlang.org/thread/rpcqefmyqigftxsgsqui forum.dlang.org), 
 I got the question whether it is possible to restrict "alias 
 this"?
 Similar to templates function, e.g. "void foo(T)(T value) if 
 (isNumeric!(T) ...", but it should be limited here so that 
 it does not interact with the cast or something other.
 As far as I know, "alias this" is an implicit cast. And just 
 like an explicit cast (opCast) it should be possible to 
 restrict it.

 Otherwise, I have unfortunately run out of ideas how I can 
 allow an implicit conversion.

What do you mean restrict it?

I mean that you can limit it similar as you can with templates. In my code i'm using "alias this" to enable an implicit conversion from any class, e.g. Foo, to Ref!Foo. Ref is an struct which checks if the value of Foo is null. If it's null a error message will let you know, that you have a problem with one of your objects (i hate the access violation message, that's the original reason for that). But if i cast with a class, that implements the implicit conversion, as in the following: [code] class A { mixin TRef!(A); } class B : A { } A a1 = new B(); B b1 = cast(B) a1.access; // <- problem [/code] the cast will implicit convert with "alias this" to Ref!B. And that isn't what i want and neither what should happen. In this case a limit to "alias this" would be great. A limitation, that "alias this" must not convert the cast to Ref!B.

But that's not what happens. a1's compile-time type is A, so the compiler looks up 'access' in A, but finds nothing. So it tries the alias this, gets a Ref!A, and calls access on that. At this point, T is A, so access returns an A. Then, the compiler tries to convert that A to a B, and gets confused. This is absolutely a bug. In fact, it is the bug Jesse Phillips reported[1]. [1]: http://d.puremagic.com/issues/show_bug.cgi?id=8001

Yes, I've just noticed that. Are there already workarounds for that problem?
Apr 29 2012