IsExpression question
Stewart Gordon
smjg_1998 at yahoo.com
Sun Jul 16 07:26:46 PDT 2006
Sean Kelly wrote:
> Kirk McDonald wrote:
>> Even after writing most of a library founded on the thing, I still
>> don't get something. What exactly is the expression
>>
>> is(T == function)
>>
>> testing? I would expect it to test whether T is a function pointer
>> type (like how "is(T == delegate)" tests whether T is a delegate
>> type), but it doesn't.
>
> It's testing whether T is a function type, not a function pointer type.
> For template code, it's mostly useful in this sort of situation:
>
> template isFunctionType( alias ref ) {
> const bool isFunctionType = is( ref == function );
> }
>
> Though it can be used to detect a function pointer type like so:
>
> template isFunctionPointerType( T ) {
> const bool isFunctionPointerType = is( typeof(*T) == function );
> }
<snip>
The spec states:
"If TypeSpecialization is one of typedef struct union class
interface enum function delegate then the condition is satisifed if
Type is one of those."
However, that doesn't excuse the behaviour of function being
inconsistent with the behaviour of delegate. This would make more sense:
template isFunctionPointerType(T) {
const bool isFunctionPointerType = is(T == function);
}
template isFunction(alias ref) {
const bool isFunction = is(typeof(&ref) == function);
}
But I do wonder what you can really do with a function in a template
without knowing at least how many parameters it has.
Indeed, the function as a kind of data type ought not to exist in D. At
the moment, it appears to exist only in the form of IsExpression that
you've used. We have function pointers, indicated by the function
keyword, and function delegates, indicated by the delegate keyword. So
whatever is(... == delegate) does, is(... == function) logically ought
to act correspondingly.
Stewart.
More information about the Digitalmars-d
mailing list