Inconsistency with function pointers
Russel Winder
russel at winder.org.uk
Sat Aug 4 09:41:18 PDT 2012
Jacob,
On Sat, 2012-08-04 at 18:02 +0200, Jacob Carlborg wrote:
> On 2012-08-04 17:06, Russel Winder wrote:
> > I am sure I am just missing something simple, but I need the nudge…
> >
> > I can do:
> >
> > import core.thread ;
> > import std.stdio ;
> >
> > int main(immutable string[] args) {
> > auto f() { return delegate () { writeln("Hello World."); }; }
Changing this to function makes no difference to the fact that this
works.
> > auto t = new Thread(f);
Put &f here and the compiler says:
threadFunctionPointerTrial.d(6): Error: constructor core.thread.Thread.this (void function() fn, ulong sz = cast(ulong)0) is not callable using argument types (void function() delegate())
threadFunctionPointerTrial.d(6): Error: cannot implicitly convert expression (&f) of type void function() delegate() to void delegate()
Failed: 'dmd' '-v' '-o-' 'threadFunctionPointerTrial.d' '-I.'
> > t.start();
> > t.join();
> > return 0;
> > }
> >
> > it is Thread(f) rather than Thread(&f) because f is a function returning
> > a void delegate() rather than being a void(). However:
> >
> > import core.thread ;
> > import std.stdio ;
> >
> > int main(immutable string[] args) {
> > auto t = new Thread( delegate () { return delegate () { writeln("Hello World."); }; } ) ;
> > t.start();
> > t.join();
> > return 0;
> > }
> >
> > trial.d(7): Error: constructor core.thread.Thread.this (void function()
> > fn, ulong sz = cast(ulong)0) is not callable using argument types (void
> > delegate() delegate() pure nothrow @safe)
> > Failed: 'dmd' '-v' '-o-' 'trial.d' '-I.'
> >
> > So I cannot use an anonymous delegate where I can use a named delegate?
>
> If you take a look at the declaration of the constructor for "Thread"
> you can see that it expects a function pointer. I'm not sure what's
> happening in the first example. I don't think any of the above examples
> should work.
Isn't there an overload for function and one for delegate?
threadFunctionPointerUnnamed.d(5): Error: constructor core.thread.Thread.this called with argument types:
((void function()))
matches both:
core.thread.Thread.this(void function() fn, ulong sz = cast(ulong)0)
and:
core.thread.Thread.this(void delegate() dg, ulong sz = cast(ulong)0)
Failed: 'dmd' '-v' '-o-' 'threadFunctionPointerUnnamed.d' '-I.'
--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder at ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: russel at winder.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20120804/f4144d9a/attachment.pgp>
More information about the Digitalmars-d-learn
mailing list