How to distinguish a delegate from a function?

Moritz Warning moritzwarning at web.de
Sun Jul 13 04:31:30 PDT 2008


On Sun, 13 Jul 2008 09:43:41 +0300, Max Samukha wrote:

> On Sat, 12 Jul 2008 21:11:46 +0000 (UTC), Moritz Warning
> <moritzwarning at web.de> wrote:
> 
>>On Sun, 13 Jul 2008 06:03:53 +0900, Bill Baxter wrote:
>>
>>> Moritz Warning wrote:
>>>> I try to check (at compile time) if an expression need to be called
>>>> with a context pointer or without.
>>>> 
>>>> E.g.: I want to tell the difference between A.foo (no context pointer
>>>> needed) and A.bar.
>>>> 
>>>> class A
>>>> {
>>>>    static void foo() {}
>>>>    void bar() {}
>>>> }
>>>> 
>>>> Any ideas?
>>> 
>>> I think these work:
>>> static if(is(xxx == function))
>>> static if(is(xxx == delegate))
>>> 
>>> --bb
>>
>>Doesn't seem to work.
>>Here are some of my tries (all evaluate to false):
>>
>>static if (is(Foo.get == delegate))
>>static if (is(Foo.init.get == delegate)) static if (is(typeof(Foo.get)
>>== delegate)) static if (is(typeof(&Foo.get) == delegate))
> 
> I wish there was no inconsistency between delegate and function type
> specializations:
> 
> class Foo
> {
>    void foo() // typeof is void delegate() {
>    }
>    
>    static void bar()// typeof is void function() {
>    }
> }
> 
> static assert(is(typeof(&Foo.init.foo) == delegate)); - ok
> 
> static assert(is(typeof(&Foo.bar) == delegate)); - ok, though &Foo.bar
> is not a delegate. why is that?
> 
> static assert(is(typeof(&Foo.bar) == function)); - fails because
> function type specialization checks for function type, not for function
> pointer type, which is imo inconsistent both with delegate type
> specialization and with function pointer declarations

Yes, I noticed that is(typeof(&Foo.bar) == delegate) is true even it is 
not an delegate.
It's confusing, maybe it's a bug?



More information about the Digitalmars-d mailing list