www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compile time checks and alias this

reply "Andrea Fontana" <nospam example.com> writes:
I have something like this:

enum isValid(T) = is(T == MyClass1);

class MyClass1 {}

class MyContainer(T)
{
    T value;
    alias value this;
}

void main()
{
	assert(isValid!MyClass1, "MyClass1 isn't valid");
	assert(isValid!(MyContainer!MyClass1), "MyContainer!MyClass1 
isn't valid");
}

second assert conditions is false. But in normal code 
MyContainer!Myclass1 is casted automagically to MyClass1 and 
works almost as MyClass1.

Is there a way to check this at compile time?
Dec 17 2013
parent reply "anonymous" <anonymous example.com> writes:
On Tuesday, 17 December 2013 at 16:50:18 UTC, Andrea Fontana 
wrote:
 I have something like this:

 enum isValid(T) = is(T == MyClass1);

 class MyClass1 {}

 class MyContainer(T)
 {
    T value;
    alias value this;
 }

 void main()
 {
 	assert(isValid!MyClass1, "MyClass1 isn't valid");
 	assert(isValid!(MyContainer!MyClass1), "MyContainer!MyClass1 
 isn't valid");
 }

 second assert conditions is false. But in normal code 
 MyContainer!Myclass1 is casted automagically to MyClass1 and
 works almost as MyClass1.

 Is there a way to check this at compile time?
is(T : MyClass1)
Dec 17 2013
parent reply "Andrea Fontana" <nospam example.com> writes:
On Tuesday, 17 December 2013 at 17:19:26 UTC, anonymous wrote:
 On Tuesday, 17 December 2013 at 16:50:18 UTC, Andrea Fontana 
 wrote:
 I have something like this:

 enum isValid(T) = is(T == MyClass1);

 class MyClass1 {}

 class MyContainer(T)
 {
   T value;
   alias value this;
 }

 void main()
 {
 	assert(isValid!MyClass1, "MyClass1 isn't valid");
 	assert(isValid!(MyContainer!MyClass1), "MyContainer!MyClass1 
 isn't valid");
 }

 second assert conditions is false. But in normal code 
 MyContainer!Myclass1 is casted automagically to MyClass1 and
 works almost as MyClass1.
Hey didn't this mean "is derived from"? :)
 Is there a way to check this at compile time?
is(T : MyClass1)
Dec 17 2013
parent "Chris Cain" <clcain uncg.edu> writes:
On Tuesday, 17 December 2013 at 21:28:12 UTC, Andrea Fontana 
wrote:
 Hey didn't this mean "is derived from"? :)
 Is there a way to check this at compile time?
is(T : MyClass1)
It actually means "is implicitly convertable to", which "is derived from" satisfies.
Dec 17 2013