How to distinguish a delegate from a function?

Max Samukha mail at nowhere.com
Sat Jul 12 23:43:41 PDT 2008


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



More information about the Digitalmars-d mailing list