www.digitalmars.com         C & C++   DMDScript  

D - function pointers

reply "Pavel Minayev" <evilone omen.ru> writes:
Yes, I know it's not the first time this question is
asked, still... what will be the syntax for function
pointers in D? Will it be like in C (please, no!),
or D-style when all type attributes are on the left...
or whatever?

The reason why I ask is that I've started messing
with WinAPI headers, trying to convert them to D, so
far I have a simple converter already but I need to
know syntax for function pointers to bring it to
working stage...
Dec 23 2001
parent reply "Walter" <walter digitalmars.com> writes:
At the moment they work like C function pointers. -Walter

"Pavel Minayev" <evilone omen.ru> wrote in message
news:a05amk$278o$1 digitaldaemon.com...
 Yes, I know it's not the first time this question is
 asked, still... what will be the syntax for function
 pointers in D? Will it be like in C (please, no!),
 or D-style when all type attributes are on the left...
 or whatever?

 The reason why I ask is that I've started messing
 with WinAPI headers, trying to convert them to D, so
 far I have a simple converter already but I need to
 know syntax for function pointers to bring it to
 working stage...
Dec 23 2001
next sibling parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:a05cv8$28l0$1 digitaldaemon.com...

 At the moment they work like C function pointers. -Walter
At the moment?.. Are you planning to change 'em?
Dec 23 2001
parent reply "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a05gga$2b5c$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:a05cv8$28l0$1 digitaldaemon.com...

 At the moment they work like C function pointers. -Walter
At the moment?.. Are you planning to change 'em?
Probably not. If it ain't broke ...
Dec 23 2001
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:a05jv9$2cuo$2 digitaldaemon.com...

 At the moment they work like C function pointers. -Walter
At the moment?.. Are you planning to change 'em?
Probably not. If it ain't broke ...
Pretty bad... it doesn't suite the way other things are declared. Both pointers and arrays in declaration are applied to type rather than to variable, why such a mess with functions? The syntax is clumsy and rather limited. How do I declare an array of function pointers? Or pointer to function that returns pointer to function? Or simply declare several pointers at once? Yes, this can be done with a typedef, I know, but with D-style syntax it would be even simplier: LRESULT(uint, HANDLE, WPARAM, LPARAM) OldWndProc; // declare function ptr void(int*)[10] ptrarray; // array of funcptrs void(int)(char[]) funcptr; // ptr to function that returns ptr to function char[]() a, b, c; // three pointers to function returning char[] Another problem with C-style syntax is typedefing and aliasing. The usual syntax is "typedef old-type new-type", the same with alias. However, function pointers are a mess here: typedef LRESULT (*WindowProc)(uint, HANDLE, WPARAM, LPARAM); It's not very easy to tell where is the "old" type and where is the "new" one by looking at this line quickly... D-style would be: typedef LRESULT(uint, HANDLE, WPARAM, LPARAM) WindowProc; Don't you think it's just better to parse, read, and understand?
Dec 23 2001
parent "Walter" <walter digitalmars.com> writes:
I have to rewrite the declaration parsing code anyway, I'll revisit it
then. -Walter

"Pavel Minayev" <evilone omen.ru> wrote in message
news:a06pcm$9bk$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:a05jv9$2cuo$2 digitaldaemon.com...

 At the moment they work like C function pointers. -Walter
At the moment?.. Are you planning to change 'em?
Probably not. If it ain't broke ...
Pretty bad... it doesn't suite the way other things are declared. Both pointers and arrays in declaration are applied to type rather than to variable, why such a mess with functions? The syntax is clumsy and rather limited. How do I declare an array of function pointers? Or pointer to function that returns pointer to function? Or simply declare several pointers at once? Yes, this can be done with a typedef, I know, but with D-style syntax it would be even simplier: LRESULT(uint, HANDLE, WPARAM, LPARAM) OldWndProc; // declare
function
 ptr
     void(int*)[10] ptrarray;    // array of funcptrs
     void(int)(char[]) funcptr;    // ptr to function that returns ptr to
 function
     char[]() a, b, c;    // three pointers to function returning char[]

 Another problem with C-style syntax is typedefing and aliasing.
 The usual syntax is "typedef old-type new-type", the same with
 alias. However, function pointers are a mess here:

     typedef LRESULT (*WindowProc)(uint, HANDLE, WPARAM, LPARAM);

 It's not very easy to tell where is the "old" type and where
 is the "new" one by looking at this line quickly... D-style
 would be:

     typedef LRESULT(uint, HANDLE, WPARAM, LPARAM) WindowProc;

 Don't you think it's just better to parse, read, and understand?
Dec 24 2001
prev sibling parent reply "Pavel Minayev" <evilone omen.ru> writes:
Also, I've noticed that there is no way to put extern()
into function pointer declaration. There should be a
way to do that since all API callbacks are defined as
stdcall.
Dec 23 2001
parent reply "Walter" <walter digitalmars.com> writes:
The usual way to accomplish that is put the extern around a typedef.

"Pavel Minayev" <evilone omen.ru> wrote in message
news:a05got$2b6c$1 digitaldaemon.com...
 Also, I've noticed that there is no way to put extern()
 into function pointer declaration. There should be a
 way to do that since all API callbacks are defined as
 stdcall.
Dec 23 2001
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:a05jv8$2cuo$1 digitaldaemon.com...

 The usual way to accomplish that is put the extern around a typedef.
Is it already possible in latest alpha?
Dec 24 2001
parent "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a06pco$9bk$2 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:a05jv8$2cuo$1 digitaldaemon.com...

 The usual way to accomplish that is put the extern around a typedef.
Is it already possible in latest alpha?
It should work.
Dec 24 2001