www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How to distinguish a delegate from a function?

reply Moritz Warning <moritzwarning web.de> writes:
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?
Jul 12 2008
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Jul 12 2008
next sibling parent reply Moritz Warning <moritzwarning web.de> writes:
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))
Jul 12 2008
parent reply Max Samukha <mail nowhere.com> writes:
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
Jul 12 2008
parent Max Samukha <samukha voliacable.com.removethis> writes:
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.
Jul 13 2008
prev sibling parent Moritz Warning <moritzwarning web.de> writes:
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?
Jul 13 2008
prev sibling parent reply Oliver Dathe <o.dathe gmx.de> writes:
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)));
}
Jul 12 2008
next sibling parent reply Moritz Warning <moritzwarning web.de> writes:
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)
Jul 12 2008
parent Matthias Walter <Matthias.Walter st.ovgu.de> writes:
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
Jul 13 2008
prev sibling parent Moritz Warning <moritzwarning web.de> writes:
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.
Jul 14 2008