digitalmars.D - Casting delegates to void*
- "David Nadlinger" <see klickverbot.at> Mar 13 2013
- Iain Buclaw <ibuclaw ubuntu.com> Mar 13 2013
- "Daniel Murphy" <yebblies nospamgmail.com> Mar 13 2013
- Walter Bright <newshound2 digitalmars.com> Mar 15 2013
- "David Nadlinger" <see klickverbot.at> Mar 16 2013
Hi all,
A quick quiz: Does the following function compile, and if yes,
what will it return?
---
void* delegateToPtr(void delegate() dg) {
return cast(void*)dg;
}
---
I am working on weeding out the last test suite failures in the
2.062 branch of LDC right now (release to be expected soon(tm)),
and was surprised to find that current DMD indeed accepts the
snippet. The result of the cast is the context pointer of the
delegate.
LDC, on the other hand, refuses to compile the cast. It would be
trivial to change its behavior to match DMD, but I am not sure
why that behavior would be desirable in the first place. It seems
rather like an accepts-invalid bug to me.
An instance of such a cast has been added to the test suite
while fixing issue 7159:
https://github.com/D-Programming-Language/dmd/blob/18da33b4d303f1dd020576a6a671f91fd6b06c10/test/runnable/xtest46.d#L5011
David
Mar 13 2013
--0015174c3b1ca846eb04d7d43142 Content-Type: text/plain; charset=ISO-8859-1 On 13 March 2013 19:18, David Nadlinger <see klickverbot.at> wrote:Hi all, A quick quiz: Does the following function compile, and if yes, what will it return? --- void* delegateToPtr(void delegate() dg) { return cast(void*)dg; } ---
{ return <retval> = dg.object; } I think it would be better to be explicit in the case though. eg: --- // Returns context. void* delegateToPtr(void delegate() dg) { return cast(void*)dg.ptr; } // Returns function pointer. void* delegateToPtr(void delegate() dg) { return cast(void*)dg.funcptr; } --- So you aren't left guessing which one it could be. Regards -- Iain Buclaw *(p < e ? p++ : p) = (c & 0x0f) + '0'; --0015174c3b1ca846eb04d7d43142 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On 1= 3 March 2013 19:18, David Nadlinger <span dir=3D"ltr"><<a href=3D"mailto= :see klickverbot.at" target=3D"_blank">see klickverbot.at</a>></span> wr= ote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex= ;border-left:1px solid rgb(204,204,204);padding-left:1ex"> Hi all,<br> <br> A quick quiz: Does the following function compile, and if yes,<br> what will it return?<br> <br> ---<br> void* delegateToPtr(void delegate() dg) {<br> =A0 =A0 return cast(void*)dg;<br> }<br> ---<br> <br></blockquote><div><br></div><div>GDC's is also:<br><br>{<br>=A0 ret= urn <retval> =3D dg.object;<br>}<br><br></div><div>I think it would b= e better to be explicit in the case though.<br><br></div><div>eg:<br>---<br=
ate() dg) {<br>=A0=A0=A0 return cast(void*)dg.ptr;<br>}<br><br></div><div>/= / Returns function pointer.<br></div><div>void* delegateToPtr(void delegate= () dg) {<br> =A0=A0=A0 return cast(void*)dg.funcptr;<br>}<br>---<br></div></div><br></di= v><div class=3D"gmail_extra">So you aren't left guessing which one it c= ould be.<br></div><div class=3D"gmail_extra"><br><br></div><div class=3D"gm= ail_extra"> Regards<br></div><div class=3D"gmail_extra">-- <br>Iain Buclaw<br><br>*(p &= lt; e ? p++ : p) =3D (c & 0x0f) + '0'; </div></div> --0015174c3b1ca846eb04d7d43142--
Mar 13 2013
"David Nadlinger" <see klickverbot.at> wrote in message news:kibnaskimiqnmzziegjo forum.dlang.org...Hi all, A quick quiz: Does the following function compile, and if yes, what will it return? --- void* delegateToPtr(void delegate() dg) { return cast(void*)dg; } ---
IIRC dg.ptr will be lowered to this, and that is how the glue layer knows you want to extract the pointer. My guess is this predates the .ptr property, and much like the array -> ulong cast, should be squashed.
Mar 13 2013
On 3/13/2013 7:32 PM, Daniel Murphy wrote:"David Nadlinger" <see klickverbot.at> wrote in message news:kibnaskimiqnmzziegjo forum.dlang.org...Hi all, A quick quiz: Does the following function compile, and if yes, what will it return? --- void* delegateToPtr(void delegate() dg) { return cast(void*)dg; } ---
IIRC dg.ptr will be lowered to this, and that is how the glue layer knows you want to extract the pointer. My guess is this predates the .ptr property, and much like the array -> ulong cast, should be squashed.
I agree. Please file an enhancement request for this in bugzilla.
Mar 15 2013
On Friday, 15 March 2013 at 21:01:26 UTC, Walter Bright wrote:On 3/13/2013 7:32 PM, Daniel Murphy wrote:"David Nadlinger" <see klickverbot.at> wrote in message news:kibnaskimiqnmzziegjo forum.dlang.org...Hi all, A quick quiz: Does the following function compile, and if yes, what will it return? --- void* delegateToPtr(void delegate() dg) { return cast(void*)dg; } ---
IIRC dg.ptr will be lowered to this, and that is how the glue layer knows you want to extract the pointer. My guess is this predates the .ptr property, and much like the array -> ulong cast, should be squashed.
I agree. Please file an enhancement request for this in bugzilla.
http://d.puremagic.com/issues/show_bug.cgi?id=9735 David
Mar 16 2013









Iain Buclaw <ibuclaw ubuntu.com> 