digitalmars.D.learn - Using delegates in callbacks for extern(C) functions
- simendsjo (16/16) Feb 24 2012 I have a C function taking a callback function as a parameter. My though...
- Timon Gehr (4/20) Feb 25 2012 This is a bug. '() {}' should be inferred as void function()pure nothrow...
- simendsjo (2/27) Feb 25 2012 Thanks. http://d.puremagic.com/issues/show_bug.cgi?id=7585
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (6/31) Feb 27 2012 Not according to the spec: "If the keywords function or delegate are
- Jonathan M Davis (5/40) Feb 27 2012 TDPL says that it's inferred, and I believe that work was recently done ...
I have a C function taking a callback function as a parameter. My thought was to wrap this up using a template, but I cannot get it to work: extern(C) alias void function() Callback; template Wrap(alias dg) { extern(C) void Wrap() { dg(); } } void main() { Callback cb = &Wrap!( () {} ); } Error: cannot implicitly convert expression (&Wrap) of type void delegate() pure nothrow safe to extern (C) void function()
Feb 24 2012
On 02/24/2012 07:22 PM, simendsjo wrote:I have a C function taking a callback function as a parameter. My thought was to wrap this up using a template, but I cannot get it to work: extern(C) alias void function() Callback; template Wrap(alias dg) { extern(C) void Wrap() { dg(); } } void main() { Callback cb = &Wrap!( () {} ); } Error: cannot implicitly convert expression (&Wrap) of type void delegate() pure nothrow safe to extern (C) void function()This is a bug. '() {}' should be inferred as void function()pure nothrow safe. Workaround: The code compiles if you use function() {} instead. I don't know if a bug report exists already.
Feb 25 2012
On Sat, 25 Feb 2012 17:12:50 +0100, Timon Gehr <timon.gehr gmx.ch> wrote:On 02/24/2012 07:22 PM, simendsjo wrote:Thanks. http://d.puremagic.com/issues/show_bug.cgi?id=7585I have a C function taking a callback function as a parameter. My thought was to wrap this up using a template, but I cannot get it to work: extern(C) alias void function() Callback; template Wrap(alias dg) { extern(C) void Wrap() { dg(); } } void main() { Callback cb = &Wrap!( () {} ); } Error: cannot implicitly convert expression (&Wrap) of type void delegate() pure nothrow safe to extern (C) void function()This is a bug. '() {}' should be inferred as void function()pure nothrow safe. Workaround: The code compiles if you use function() {} instead. I don't know if a bug report exists already.
Feb 25 2012
On 02/25/2012 08:12 AM, Timon Gehr wrote:On 02/24/2012 07:22 PM, simendsjo wrote:Not according to the spec: "If the keywords function or delegate are omitted, it defaults to being a delegate" http://dlang.org/expression.html#FunctionLiteralI have a C function taking a callback function as a parameter. My thought was to wrap this up using a template, but I cannot get it to work: extern(C) alias void function() Callback; template Wrap(alias dg) { extern(C) void Wrap() { dg(); } } void main() { Callback cb = &Wrap!( () {} ); } Error: cannot implicitly convert expression (&Wrap) of type void delegate() pure nothrow safe to extern (C) void function()This is a bug. '() {}' should be inferred as void function()pure nothrow safe.Workaround: The code compiles if you use function() {} instead.If I read the spec correctly, one must use the 'function' keyword. Ali
Feb 27 2012
On Monday, February 27, 2012 16:42:06 Ali Çehreli wrote:On 02/25/2012 08:12 AM, Timon Gehr wrote:TDPL says that it's inferred, and I believe that work was recently done in improving the compiler's ability to infer it. So, in this case, the spec is almost certainly wrong. - Jonathan M DavisOn 02/24/2012 07:22 PM, simendsjo wrote:Not according to the spec: "If the keywords function or delegate are omitted, it defaults to being a delegate" http://dlang.org/expression.html#FunctionLiteralI have a C function taking a callback function as a parameter. My thought was to wrap this up using a template, but I cannot get it to work: extern(C) alias void function() Callback; template Wrap(alias dg) { extern(C) void Wrap() { dg(); } } void main() { Callback cb = &Wrap!( () {} ); } Error: cannot implicitly convert expression (&Wrap) of type void delegate() pure nothrow safe to extern (C) void function()This is a bug. '() {}' should be inferred as void function()pure nothrow safe.Workaround: The code compiles if you use function() {} instead.If I read the spec correctly, one must use the 'function' keyword.
Feb 27 2012