www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - why are types mismatch?

reply "Roman" <shamyan.roman gmail.com> writes:
alias int function(int) function_type;



void main()
{
	bar!foo(2);
	bar!((int i)=> i*2)(2);
}

int foo(int i)
{
     return i;
}

void bar(alias baz)(int i)
{
	static if (!is(typeof(baz) == function_type))
	{
		pragma(msg, typeof(baz), " != ", function_type); //wtf?
	}

	std.stdio.writeln(bar(i));
}

Where am I wrong?
Oct 01 2013
next sibling parent "deadalnix" <deadalnix gmail.com> writes:
On Tuesday, 1 October 2013 at 18:07:31 UTC, Roman wrote:
 alias int function(int) function_type;



 void main()
 {
 	bar!foo(2);
 	bar!((int i)=> i*2)(2);
 }

 int foo(int i)
 {
     return i;
 }

 void bar(alias baz)(int i)
 {
 	static if (!is(typeof(baz) == function_type))
 	{
 		pragma(msg, typeof(baz), " != ", function_type); //wtf?
 	}

 	std.stdio.writeln(bar(i));
 }

 Where am I wrong?

bar!foo should work as you expect. bar!((int i)=> i*2) is different because (int i) => i*2 is probably a template. Can you paste the output of your code so we can help you more easily ?
Oct 01 2013
prev sibling next sibling parent "David Nadlinger" <code klickverbot.at> writes:
On Tuesday, 1 October 2013 at 18:10:05 UTC, deadalnix wrote:
 bar!((int i)=> i*2) is different because (int i) => i*2 is 
 probably a template.

Also, it will be inferred as pure, nothrow and safe. David
Oct 01 2013
prev sibling next sibling parent "Roman" <shamyan.roman gmail.com> writes:
 Can you paste the output of your code so we can help you more 
 easily ?

int(int i) != int function(int) int function(int i) pure nothrow safe != int function(int) 2 4 So, why 1st case incorrect?
Oct 01 2013
prev sibling next sibling parent "deadalnix" <deadalnix gmail.com> writes:
On Tuesday, 1 October 2013 at 18:38:33 UTC, Roman wrote:
 Can you paste the output of your code so we can help you more 
 easily ?

int(int i) != int function(int) int function(int i) pure nothrow safe != int function(int) 2 4 So, why 1st case incorrect?

The first case is a bug to me. Can you fill it in http://d.puremagic.com/issues/ please ? The second is expected. What you should do is test via is(typeof(...)) : function_type). The : test for implicit conversion, and int function(int i) pure nothrow safe convert implicitly to int function(int) .
Oct 01 2013
prev sibling next sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Tuesday, 1 October 2013 at 18:07:31 UTC, Roman wrote:
 alias int function(int) function_type;

function_type is a function pointer
 void main()
 {
 	bar!foo(2);

foo is a function, not a function pointer
 	bar!((int i)=> i*2)(2);
 }

 int foo(int i)
 {
     return i;
 }

 void bar(alias baz)(int i)
 {
 	static if (!is(typeof(baz) == function_type))
 	{
 		pragma(msg, typeof(baz), " != ", function_type); //wtf?
 	}

here you are testing that baz is a function pointer
 	std.stdio.writeln(bar(i));

you probably meant baz here
 }

 Where am I wrong?

Wrong at place where you mix function type and function pointer. You are facing int(int i) != int function(int) int function(int i) pure nothrow safe != int function(int)
Oct 01 2013
prev sibling next sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Tuesday, 1 October 2013 at 18:10:05 UTC, deadalnix wrote:
 On Tuesday, 1 October 2013 at 18:07:31 UTC, Roman wrote:
 alias int function(int) function_type;



 void main()
 {
 	bar!foo(2);
 	bar!((int i)=> i*2)(2);
 }

 int foo(int i)
 {
    return i;
 }

 void bar(alias baz)(int i)
 {
 	static if (!is(typeof(baz) == function_type))
 	{
 		pragma(msg, typeof(baz), " != ", function_type); //wtf?
 	}

 	std.stdio.writeln(bar(i));
 }

 Where am I wrong?

bar!foo should work as you expect. bar!((int i)=> i*2) is different because (int i) => i*2 is probably a template. Can you paste the output of your code so we can help you more easily ?

??? foo is not a function pointer auto x = &foo; bar!(x)(2); should work. And (int i) => i*2 is of course not a template [(i) => i*2 would be].
Oct 01 2013
prev sibling next sibling parent "Roman" <shamyan.roman gmail.com> writes:
On Tuesday, 1 October 2013 at 18:46:45 UTC, deadalnix wrote:
 On Tuesday, 1 October 2013 at 18:38:33 UTC, Roman wrote:
 Can you paste the output of your code so we can help you more 
 easily ?

int(int i) != int function(int) int function(int i) pure nothrow safe != int function(int) 2 4 So, why 1st case incorrect?

The first case is a bug to me. Can you fill it in http://d.puremagic.com/issues/ please ? The second is expected. What you should do is test via is(typeof(...)) : function_type). The : test for implicit conversion, and int function(int i) pure nothrow safe convert implicitly to int function(int) .

With ":" bar!((int i)=> i*2)(2) works properly, but bar!foo(2) still doesn't pass expression ( evidently foo doesn't implicitly convert to function_type)
Oct 01 2013
prev sibling next sibling parent "Roman" <shamyan.roman gmail.com> writes:
 you probably meant baz here

 auto x = &foo;
 bar!(x)(2);

 should work.

Yeah, it worked! Thanks!
Oct 01 2013
prev sibling next sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Tuesday, 1 October 2013 at 19:11:26 UTC, Roman wrote:
 On Tuesday, 1 October 2013 at 18:46:45 UTC, deadalnix wrote:
 On Tuesday, 1 October 2013 at 18:38:33 UTC, Roman wrote:
 Can you paste the output of your code so we can help you 
 more easily ?

int(int i) != int function(int) int function(int i) pure nothrow safe != int function(int) 2 4 So, why 1st case incorrect?

The first case is a bug to me. Can you fill it in http://d.puremagic.com/issues/ please ? The second is expected. What you should do is test via is(typeof(...)) : function_type). The : test for implicit conversion, and int function(int i) pure nothrow safe convert implicitly to int function(int) .

With ":" bar!((int i)=> i*2)(2) works properly, but bar!foo(2) still doesn't pass expression ( evidently foo doesn't implicitly convert to function_type)

You are not in C, where 'function_name' (except some circumstances) is implicitly converted to pointer to that function.
Oct 01 2013
prev sibling parent "deadalnix" <deadalnix gmail.com> writes:
On Tuesday, 1 October 2013 at 18:55:42 UTC, Maxim Fomin wrote:
 foo is not a function pointer

Hoooo ! I see. Well, it should. This distinction between function pointer and function do not even exists at assembly level (you always pass a function pointer to call instruction).
Oct 01 2013