Thread overview
test if object is instance of class at compile time
Dec 21, 2011
Elvis Maehren
Dec 22, 2011
Vladimir Panteleev
Dec 22, 2011
Elvis Maehren
Dec 22, 2011
Jacob Carlborg
Dec 22, 2011
Elvis Maehren
Dec 22, 2011
Timon Gehr
Dec 22, 2011
Elvis Maehren
Dec 22, 2011
Trass3r
Dec 22, 2011
Andrew Wiley
December 21, 2011
This works fine at runtime, but explodes in CTFE:
---
bool isInstanceOf(T, O)(O o) {
	return (cast(T) o) !is null;
}
---

CTFE doesn't like "!is null" ("cannot compare [...] at compile time").

Moreover, in CTFE a failed cast terminates the interpretation ("cannot reinterpret class from [...] to [...] at compile time"). Is this somehow catchable? If so, I could do something along these lines:
---
if(__ctfe) {
	try {
		auto t = cast(T) o;
		return true;
	} catch {
		return false;
	}
}
---

December 22, 2011
On Wednesday, 21 December 2011 at 20:15:36 UTC, Elvis Maehren wrote:
> Is this somehow catchable?

I believe that's what __traits(compiles, ...) is for:

http://dlang.org/traits.html#compiles
December 22, 2011
On 2011-12-21 21:15, Elvis Maehren wrote:
> This works fine at runtime, but explodes in CTFE:
> ---
> bool isInstanceOf(T, O)(O o) {
> 	return (cast(T) o) !is null;
> }
> ---
>
> CTFE doesn't like "!is null" ("cannot compare [...] at compile time").
>
> Moreover, in CTFE a failed cast terminates the interpretation ("cannot
> reinterpret class from [...] to [...] at compile time"). Is this somehow
> catchable? If so, I could do something along these lines:
> ---
> if(__ctfe) {
> 	try {
> 		auto t = cast(T) o;
> 		return true;
> 	} catch {
> 		return false;
> 	}
> }
> ---
>

I don't think you can do a test like that during compile time. In your above function, isInstanceOf, "o" will always be of the type "O". You can always do a comparisons to see if O is the same type as T.

-- 
/Jacob Carlborg
December 22, 2011
Vladimir Panteleev wrote:
> I believe that's what __traits(compiles, ...) is for:

__traits(compiles, cast(T) o) is true even if the cast blows up in CTFE, so that doesn't work.
December 22, 2011
Jacob Carlborg wrote:
> On 2011-12-21 21:15, Elvis Maehren wrote:
>> This works fine at runtime, but explodes in CTFE:
>> ---
>> bool isInstanceOf(T, O)(O o) {
>> return (cast(T) o) !is null;
>> }
>> ---
>>
[...]
> I don't think you can do a test like that during compile time. In your above function, isInstanceOf, "o" will always be of the type "O". You can always do a comparisons to see if O is the same type as T.
> 

I don't think testing for equality of O and T gets me anywhere. T is usually derived from O. That makes it a different type, but still o may be castable to T.
December 22, 2011
On 12/21/2011 09:15 PM, Elvis Maehren wrote:
> This works fine at runtime, but explodes in CTFE:
> ---
> bool isInstanceOf(T, O)(O o) {
> 	return (cast(T) o) !is null;
> }
> ---
>
> CTFE doesn't like "!is null" ("cannot compare [...] at compile time").
>

This will be fixed in the next release:

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

> Moreover, in CTFE a failed cast terminates the interpretation ("cannot
> reinterpret class from [...] to [...] at compile time").

I don't think this is filed already. Maybe you want to file a bug report?

> Is this somehow
> catchable? If so, I could do something along these lines:
> ---
> if(__ctfe) {
> 	try {
> 		auto t = cast(T) o;
> 		return true;
> 	} catch {
> 		return false;
> 	}
> }
> ---
>

use traits(__compiles, {...}), where '...' is the exact code that fails. But that is a mere workaround. What you are doing should/will work.
December 22, 2011
Timon Gehr wrote:
> On 12/21/2011 09:15 PM, Elvis Maehren wrote:
[...]
>> Moreover, in CTFE a failed cast terminates the interpretation ("cannot reinterpret class from [...] to [...] at compile time").
> 
> I don't think this is filed already. Maybe you want to file a bug report?

ok, did that: http://d.puremagic.com/issues/show_bug.cgi?id=7154

[...]
> use traits(__compiles, {...}), where '...' is the exact code that fails. But that is a mere workaround. What you are doing should/will work.

Vladimir Panteleev pointed me to __traits(compiles, ...), too. I think that doesn't work out:

Elvis Maehren wrote:
> Vladimir Panteleev wrote:
>> I believe that's what __traits(compiles, ...) is for:
> 
> __traits(compiles, cast(T) o) is true even if the cast blows up in CTFE,
> so that doesn't work.

Or am I missing something?
December 22, 2011
I'd really like to have 'if (instance is ClassType)' syntax in D.
December 22, 2011
On Thu, Dec 22, 2011 at 8:35 AM, Trass3r <un@known.com> wrote:
> I'd really like to have 'if (instance is ClassType)' syntax in D.

It couldn't use that syntax because 'is' means direct bitwise equality everywhere else. Changing it in this one situation would be awkward.