Callbacks in D as void functions

2014-11-13 Thread Wsdes via Digitalmars-d-learn
Hi all, I already posted this to Code Project, but maybe you have a solution for my problem. I am trying to write a wrapper for a C API in D. In C I have the following definition of a callback type: typedef void (*Callback)(void*); In a struct all members are of the type Callback. In the main

Re: Callbacks in D as void functions

2014-11-13 Thread bearophile via Digitalmars-d-learn
Wsdes: The wiki says that callback functions should be declared as integer functions, What's bad in returning void? Bye, bearophile

Re: Callbacks in D as void functions

2014-11-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 11/13/14 9:49 AM, Wsdes wrote: Hi all, I already posted this to Code Project, but maybe you have a solution for my problem. I am trying to write a wrapper for a C API in D. In C I have the following definition of a callback type: typedef void (*Callback)(void*); in D, this would be:

Re: Callbacks in D as void functions

2014-11-13 Thread Adam D. Ruppe via Digitalmars-d-learn
On Thursday, 13 November 2014 at 14:50:00 UTC, Wsdes wrote: I am trying to write a wrapper for a C API in D. In C I have the following definition of a callback type: typedef void (*Callback)(void*); I would translate this directly to D: extern(C) alias Callback = void function(void*);

Re: Callbacks in D as void functions

2014-11-13 Thread Wsdes via Digitalmars-d-learn
On Thursday, 13 November 2014 at 15:17:45 UTC, Adam D. Ruppe wrote: On Thursday, 13 November 2014 at 14:50:00 UTC, Wsdes wrote: I am trying to write a wrapper for a C API in D. In C I have the following definition of a callback type: typedef void (*Callback)(void*); I would translate this

Re: Callbacks in D as void functions

2014-11-13 Thread Adam D. Ruppe via Digitalmars-d-learn
On Thursday, 13 November 2014 at 15:58:34 UTC, Wsdes wrote: void MyDtaCB(void* v){ // Do stuff } Events.OnData = cast(Callback) MyDtaCB; this compiles but might crash, the extern(C) is important on a callback to be used from a C program. I'd really recommend adding that to the function,

Re: Callbacks in D as void functions

2014-11-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 11/13/14 10:58 AM, Wsdes wrote: First of all, the link to the wiki that has an example of callbacks in C and D: http://dlang.org/interfaceToC.html Secondly, I tried your code and that was exactly what I was thinking and what I tried before. Then I thought I'd turn to the wiki example, so

Re: Callbacks in D as void functions

2014-11-13 Thread Mike Parker via Digitalmars-d-learn
On 11/14/2014 12:58 AM, Wsdes wrote: Anyway, I think I got the problem solved. Well, there seems to never have been any problem as I am taught now. I asked the developer of the C API this morning if I should try to implement the callback functions redundantly in D and he said he will have a