www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can't cast to delgate type from void*?

reply Rick Mann <rmann-d-lang latencyzero.com> writes:
I'm trying to use delegates with Mac OS X's carbon events. When you register an
event handler with the system, you can pass an arbitrary 4 bytes, which are
passed back to you when the event handler is called.

So, I was hoping to use a delegate. In my handler, I try to do this:

extern (C)
OSStatus
EventHandler(EventHandlerCallRef inHandlerCallRef,
                            EventRef inEvent,
                            void* inUserData)
{
    try
    {
        EventHandlerMethod handleEvent = cast (EventHandlerMethod) inUserData;
        .
        .
        .

However, the compiler complains:

DEventHandler.d:119: error: conversion to non-scalar type requested

Why can't I do this?

TIA,
Rick
Feb 18 2007
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Rick Mann wrote:
 I'm trying to use delegates with Mac OS X's carbon events. When you register
an event handler with the system, you can pass an arbitrary 4 bytes, which are
passed back to you when the event handler is called.
 
 So, I was hoping to use a delegate. In my handler, I try to do this:
 
 extern (C)
 OSStatus
 EventHandler(EventHandlerCallRef inHandlerCallRef,
                             EventRef inEvent,
                             void* inUserData)
 {
     try
     {
         EventHandlerMethod handleEvent = cast (EventHandlerMethod) inUserData;
         .
         .
         .
 
 However, the compiler complains:
 
 DEventHandler.d:119: error: conversion to non-scalar type requested
 
 Why can't I do this?
 
 TIA,
 Rick
 
Delegates are 8 bytes. They consist of a function pointer and a context pointer. I'm not familiar with Carbon's event handling, but I suspect that void* is intended to serve the same purpose as a delegate's context pointer. -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Feb 18 2007
parent Rick Mann <rmann-d-lang latencyzero.com> writes:
Kirk McDonald Wrote:

 Delegates are 8 bytes. They consist of a function pointer and a context 
 pointer. I'm not familiar with Carbon's event handling, but I suspect 
 that void* is intended to serve the same purpose as a delegate's context 
 pointer.
Yeah, I finally figured this out. I thought the delegate was actually a reference to the 8 bytes, in the same way a class type is a reference. I just needed to take the address of the delegate, and it worked fine. Thanks!
Feb 18 2007