www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unable to pass shared class method as delegate

reply d coder <dlang.coder gmail.com> writes:
--0003255599864e313804a1ab13b6
Content-Type: text/plain; charset=ISO-8859-1

Greetings

I am facing problem passing a shared class method as an argument to a
function. Look at the following minimized code snippet.

class Foo {
  shared // compiles when commented out
    void bar() {}
}

void frop(void delegate() dg) {
}

void main() {
  shared Foo foo = new shared(Foo);
  frop(&foo.bar);
}

I get the following errors (using dmd 2.052)
dg.d(11): Error: function dg.frop (void delegate() dg) is not callable using
argument types (void delegate() shared)
dg.d(11): Error: cannot implicitly convert expression (&foo.bar) of type
void delegate() shared to void delegate()

Please suggest a valid signature for the function frop, so that it can take
shared delegates.

Regards
- Puneet

--0003255599864e313804a1ab13b6
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Greetings
<div><br></div><div>I am facing problem passing a shared class method as an=
 argument to a function. Look at the following minimized code snippet.</div=
<div><br></div><div><div>class Foo {</div><div>=A0 shared // compiles when=

<div>=A0 =A0 void bar() {}</div><div>}</div><div><br></div><div>void frop(v= oid delegate() dg) {</div><div>}</div><div><br></div><div>void main() {</di= v><div>=A0 shared Foo foo =3D new shared(Foo);</div><div>=A0 frop(&amp;foo.= bar);</div> <div>}</div></div><div><br></div><div>I get the following errors (using dmd= 2.052)</div><div><div>dg.d(11): Error: function dg.frop (void delegate() d= g) is not callable using argument types (void delegate() shared)</div> <div> dg.d(11): Error: cannot implicitly convert expression (&amp;foo.bar) of typ= e void delegate() shared to void delegate()</div></div><div><br></div><div>= Please suggest a valid signature for the function frop, so that it can take= shared delegates.</div> <div><br></div><div>Regards</div><div>- Puneet</div> --0003255599864e313804a1ab13b6--
Apr 24 2011
next sibling parent Robert Clipsham <robert octarineparrot.com> writes:
On 24/04/2011 15:40, d coder wrote:
 Greetings

 I am facing problem passing a shared class method as an argument to a
 function. Look at the following minimized code snippet.

 class Foo {
    shared // compiles when commented out
      void bar() {}
 }

 void frop(void delegate() dg) {
 }

 void main() {
    shared Foo foo = new shared(Foo);
    frop(&foo.bar);
 }

 I get the following errors (using dmd 2.052)
 dg.d(11): Error: function dg.frop (void delegate() dg) is not callable
 using argument types (void delegate() shared)
 dg.d(11): Error: cannot implicitly convert expression (&foo.bar) of type
 void delegate() shared to void delegate()

 Please suggest a valid signature for the function frop, so that it can
 take shared delegates.

 Regards
 - Puneet

I've copy and pasted my reply to Benjamin below, you should be able to adapt it to your needs: The only way I've found to do this that works is using an alias - this is probably worth a bug report if there isn't one already. ---- class A { // Note that you get forward reference errors if you opt to // infer the return type here. That's also a bug. void method(const const(char[]) str) shared { } } alias typeof(&A.method) shdg; void foo(shdg foo) { } void main() { foo(&A.method); } ---- -- Robert http://octarineparrot.com/
Apr 24 2011
prev sibling parent d coder <dlang.coder gmail.com> writes:
--0016e6dab473c79fd404a1b39db5
Content-Type: text/plain; charset=ISO-8859-1

 I've copy and pasted my reply to Benjamin below, you should be able to
 adapt it to your needs:

Stupid me. Should have checked the list before posting.
 The only way I've found to do this that works is using an alias - this is
 probably worth a bug report if there isn't one already.

I checked and found that this is already reported http://d.puremagic.com/issues/buglist.cgi?quicksearch=shared+delegate And since Walter says that const issues are high on his priority list, I hope this would get resolved soon. Regards - Puneet --0016e6dab473c79fd404a1b39db5 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=3D"margi= n:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div class= =3D"h5"><br></div></div> I&#39;ve copy and pasted my reply to Benjamin below, you should be able to = adapt it to your needs:<br></blockquote><div><br></div><div>Stupid me. Shou= ld have checked the list before posting.</div><div>=A0</div><blockquote cla= ss=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;pa= dding-left:1ex;"> <br> The only way I&#39;ve found to do this that works is using an alias - this = is probably worth a bug report if there isn&#39;t one already.</blockquote>= <div><br></div><div>I checked and found that this is already reported =A0<a= href=3D"http://d.puremagic.com/issues/buglist.cgi?quicksearch=3Dshared+del= egate">http://d.puremagic.com/issues/buglist.cgi?quicksearch=3Dshared+deleg= ate</a></div> <div><br></div><div>And since Walter says that const issues are high on his= priority list, I hope this would get resolved soon.</div><div><br></div><d= iv>Regards</div><div>- Puneet</div><div><br></div></div> --0016e6dab473c79fd404a1b39db5--
Apr 24 2011