www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Inconsistency with function pointers

reply Russel Winder <russel winder.org.uk> writes:
I am sure I am just missing something simple, but I need the nudge=E2=80=A6

I can do:

        import core.thread ;
        import std.stdio ;
       =20
        int main(immutable string[] args) {
          auto f() { return delegate () { writeln("Hello World."); }; }
          auto t =3D new Thread(f);
          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 ;
       =20
        int main(immutable string[] args) {
          auto t =3D 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 =3D 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?
--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
Aug 04 2012
next sibling parent reply Jacob Carlborg <doob me.com> writes:
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."); }; }
            auto t = new Thread(f);
            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. -- /Jacob Carlborg
Aug 04 2012
parent reply Russel Winder <russel winder.org.uk> writes:
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=E2=
=80=A6
 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 =3D new Thread(f);
Put &f here and the compiler says: threadFunctionPointerTrial.d(6): Error: constructor core.thread.Thread.this= (void function() fn, ulong sz =3D cast(ulong)0) is not callable using argu= ment types (void function() delegate()) threadFunctionPointerTrial.d(6): Error: cannot implicitly convert expressio= n (&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 returnin=
g
 a void delegate() rather than being a void(). However:

          import core.thread ;
          import std.stdio ;

          int main(immutable string[] args) {
            auto t =3D new Thread( delegate () { return delegate () { wr=
iteln("Hello World."); }; } ) ;
            t.start();
            t.join();
            return 0;
          }

 trial.d(7): Error: constructor core.thread.Thread.this (void function()
 fn, ulong sz =3D cast(ulong)0) is not callable using argument types (vo=
id
 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?
=20 If you take a look at the declaration of the constructor for "Thread"=20 you can see that it expects a function pointer. I'm not sure what's=20 happening in the first example. I don't think any of the above examples=
=20
 should work.
Isn't there an overload for function and one for delegate? threadFunctionPointerUnnamed.d(5): Error: constructor core.thread.Thread.th= is called with argument types: ((void function())) matches both: core.thread.Thread.this(void function() fn, ulong sz =3D cast(ulong)0) and: core.thread.Thread.this(void delegate() dg, ulong sz =3D cast(ulong)0) Failed: 'dmd' '-v' '-o-' 'threadFunctionPointerUnnamed.d' '-I.' --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Aug 04 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-08-04 18:41, Russel Winder wrote:

 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.'
Hmm, right. I didn't scroll down far enough. Then I don't know. -- /Jacob Carlborg
Aug 04 2012
prev sibling next sibling parent "anonymous" <anonymous example.com> writes:
On Saturday, 4 August 2012 at 15:23:39 UTC, Russel Winder wrote:
[...]
 I can do:
[...]
           auto f() { return delegate () { writeln("Hello 
 World."); }; }
           auto t = new Thread(f);
[...]
 However:
[...]
           auto t = new Thread( delegate () { return delegate () 
 { writeln("Hello World."); }; } ) ;
[doesn't work] You need parentheses to call the anonymous delegate: auto t = new Thread( delegate () { return delegate () { writeln("Hello World."); }; }()) ; You don't need them with f, because there you have to write &f to refer to the delegate itself. (And because you don't use -property.)
Aug 04 2012
prev sibling parent reply "David Nadlinger" <see klickverbot.at> writes:
On Saturday, 4 August 2012 at 15:23:39 UTC, Russel Winder wrote:
 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?
You are trying to pass a delegate which returns a delegate – this isn't going to work. ;) In your first example is that f is implicitly called due to the non-strict property syntax. David
Aug 04 2012
next sibling parent "David Nadlinger" <see klickverbot.at> writes:
On Saturday, 4 August 2012 at 19:03:30 UTC, David Nadlinger wrote:
 In your first example is that […]
Darn, make this »In your first example, …«.
Aug 04 2012
prev sibling parent Russel Winder <russel winder.org.uk> writes:
On Sat, 2012-08-04 at 21:03 +0200, David Nadlinger wrote:
[=E2=80=A6]
 You are trying to pass a delegate which returns a delegate =E2=80=93=20
 this isn't going to work. ;)
But that is my whole point, it does a lot of the time, and it depends on whether you are working with named functions and delegates or with anonymous functions and delegates.
 In your first example is that f is implicitly called due to the=20
 non-strict property syntax.
OK, so this is looking like the nudge that anonymous was also talking about, but from a different viewpoint. It seems there is implicit behaviour going on here that is creating right royal confusion on the part of at least one programmer, me. The parameter I think I am passing to thread construction is sometimes, but not mostly, not the parameter the Thread constructors are receiving. This looks like it is going to violate the Principle of Least Surprise :-( --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Aug 04 2012