www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias and extern(C)

reply "Laeeth Isharc" <laeethnospam spammenot_laeeth.com> writes:
Hi.

I am trying to translate the following from the Dart header:
typedef void (*Dart_MessageNotifyCallback)(Dart_Isolate 
dest_isolate);

So I made a little simple test to understand callbacks in D.  The 
code below works fine if you remove the extern(C).  But I get the 
error "functionpointertest.d(6): Error: basic type expected, not 
extern" with the code as it  is.

How do I use alias with extern ?

Thanks.


Laeeth.

import std.stdio;
// test_typedef.d

//typedef void (*Dart_MessageNotifyCallback)(Dart_Isolate 
dest_isolate);

alias Callback= extern(C) void function(int);

extern(C) void testfn(int i)
{
	writefln("%s",i+1);
	return;
}

extern(C) void testfn2(int i)
{
	writefln("%s",i*i);
	return;
}

void foo(Callback callback)
//void foo(void function(int) callback)
{
	callback(100);
	//callback(101);
}

void main()
{
	foo(&testfn);
	foo(&testfn2);
}
Oct 30 2014
parent reply "anonymous" <anonymous example.com> writes:
On Thursday, 30 October 2014 at 18:43:21 UTC, Laeeth Isharc wrote:
 The code below works fine if you remove the extern(C).  But I 
 get the error "functionpointertest.d(6): Error: basic type 
 expected, not extern" with the code as it  is.

 How do I use alias with extern ?
[...]
 alias Callback= extern(C) void function(int);
Compiles as is with dmd 2.066. For 2.065 and older you have to put extern(C) in front of "alias": extern(C) alias Callback= void function(int);
Oct 30 2014
parent "Laeeth Isharc" <laeethnospam spammenot_laeeth.com> writes:
 The code below works fine if you remove the extern(C).  But I 
 get the error "functionpointertest.d(6): Error: basic type 
 expected, not extern" with the code as it  is.

 How do I use alias with extern ?
[...]
 alias Callback= extern(C) void function(int);
Compiles as is with dmd 2.066. For 2.065 and older you have to put extern(C) in front of "alias": extern(C) alias Callback= void function(int);
Many thanks, anon. You are right - I was running older dmd on this machine. It works fine with your modification, as you suggest. Laeeth.
Oct 30 2014