www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Casting delegates to void*

reply "David Nadlinger" <see klickverbot.at> writes:
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
next sibling parent Iain Buclaw <ibuclaw ubuntu.com> writes:
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;
 }
 ---
GDC's is also: { 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';
Mar 13 2013
prev sibling parent reply "Daniel Murphy" <yebblies nospamgmail.com> writes:
"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
parent reply Walter Bright <newshound2 digitalmars.com> writes:
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
parent "David Nadlinger" <see klickverbot.at> writes:
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