Thread overview
How to distinguish a delegate from a function?
Jul 12, 2008
Moritz Warning
Jul 12, 2008
Bill Baxter
Jul 12, 2008
Moritz Warning
Jul 13, 2008
Max Samukha
Jul 13, 2008
Moritz Warning
Jul 14, 2008
Max Samukha
Jul 13, 2008
Oliver Dathe
Jul 13, 2008
Moritz Warning
Jul 13, 2008
Matthias Walter
Jul 14, 2008
Moritz Warning
July 12, 2008
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?
July 12, 2008
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
July 12, 2008
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))
July 13, 2008
This works here:

template isCallableStatically(T)
{
	static if (is(T==function) || is(typeof(*T)==function))
		const bool isCallableStatically = true;
	else static if (is(T==delegate))
		const bool isCallableStatically = false;
	else static assert (false);
}

template isCallableStatically(alias T)
{
	const isCallableStatically = isCallableStatically!(typeof(&T));
}

void fn() {}

void main()
{
	void dg() {}

	static assert (isCallableStatically!(fn));
	static assert (!isCallableStatically!(dg));
	static assert (isCallableStatically!(typeof(&fn)));
	static assert (!isCallableStatically!(typeof(&dg)));
}
July 13, 2008
On Sun, 13 Jul 2008 03:38:36 +0200, Oliver Dathe wrote:

> This works here:
> 
> template isCallableStatically(T)
> {
> 	static if (is(T==function) || is(typeof(*T)==function))
> 		const bool isCallableStatically = true;
> 	else static if (is(T==delegate))
> 		const bool isCallableStatically = false;
> 	else static assert (false);
> }
> 
> template isCallableStatically(alias T) {
> 	const isCallableStatically = isCallableStatically!(typeof(&T));
> }
> 
> void fn() {}
> 
> void main()
> {
> 	void dg() {}
> 
> 	static assert (isCallableStatically!(fn)); static assert
> 	(!isCallableStatically!(dg)); static assert
> 	(isCallableStatically!(typeof(&fn))); static assert
> 	(!isCallableStatically!(typeof(&dg)));
> }

Doesn't work for A.bar and A.foo (where foo is static and therefore don't need an object)
July 13, 2008
On Sat, 12 Jul 2008 21:11:46 +0000 (UTC), Moritz Warning
<moritzwarning@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
July 13, 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@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?
July 13, 2008
Moritz Warning Wrote:

> On Sun, 13 Jul 2008 03:38:36 +0200, Oliver Dathe wrote:
> 
> > This works here:
> > 
> > template isCallableStatically(T)
> > {
> > 	static if (is(T==function) || is(typeof(*T)==function))
> > 		const bool isCallableStatically = true;
> > 	else static if (is(T==delegate))
> > 		const bool isCallableStatically = false;
> > 	else static assert (false);
> > }
> > 
> > template isCallableStatically(alias T) {
> > 	const isCallableStatically = isCallableStatically!(typeof(&T));
> > }
> > 
> > void fn() {}
> > 
> > void main()
> > {
> > 	void dg() {}
> > 
> > 	static assert (isCallableStatically!(fn)); static assert
> > 	(!isCallableStatically!(dg)); static assert
> > 	(isCallableStatically!(typeof(&fn))); static assert
> > 	(!isCallableStatically!(typeof(&dg)));
> > }
> 
> Doesn't work for A.bar and A.foo (where foo is static and therefore don't need an object)

I had this problem some months ago and found a solution: You can extract the information, whether a function given as a template alias parameter is static or not, from the mangle of it. Currently, I'm developing a Compile-Time-Function-Execution- and Template-Metaprogramming- Utils library. The first goal after some string-parsing routines is a compile-time demangler which will then also be able to extract this information. But I guess, I'll stillt need some weeks for this. When I'm done, I'll create a dsource projects and announce it on the news.

best regards
Matthias Walter
July 14, 2008
On Sun, 13 Jul 2008 11:31:30 +0000 (UTC), Moritz Warning
<moritzwarning@web.de> wrote:

>
>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?

The spec says nothing about exact semantics of type specializations of that kind, so it may be a feature.
July 14, 2008
On Sun, 13 Jul 2008 09:42:59 -0400, Matthias Walter wrote:

> Moritz Warning Wrote:
> 
>> On Sun, 13 Jul 2008 03:38:36 +0200, Oliver Dathe wrote:
>> 
>> > This works here:
>> > 
>> > template isCallableStatically(T)
>> > {
>> > 	static if (is(T==function) || is(typeof(*T)==function))
>> > 		const bool isCallableStatically = true;
>> > 	else static if (is(T==delegate))
>> > 		const bool isCallableStatically = false;
>> > 	else static assert (false);
>> > }
>> > 
>> > template isCallableStatically(alias T) {
>> > 	const isCallableStatically = isCallableStatically!(typeof(&T));
>> > }
>> > 
>> > void fn() {}
>> > 
>> > void main()
>> > {
>> > 	void dg() {}
>> > 
>> > 	static assert (isCallableStatically!(fn)); static assert
>> > 	(!isCallableStatically!(dg)); static assert
>> > 	(isCallableStatically!(typeof(&fn))); static assert
>> > 	(!isCallableStatically!(typeof(&dg)));
>> > }
>> 
>> Doesn't work for A.bar and A.foo (where foo is static and therefore
>> don't need an object)
> 
> I had this problem some months ago and found a solution: You can extract the information, whether a function given as a template alias parameter is static or not, from the mangle of it. Currently, I'm developing a Compile-Time-Function-Execution- and Template-Metaprogramming- Utils library. The first goal after some string-parsing routines is a compile-time demangler which will then also be able to extract this information. But I guess, I'll stillt need some weeks for this. When I'm done, I'll create a dsource projects and announce it on the news.
> 
> best regards
> Matthias Walter

Great! I'm looking forward to make use of the solution.