Thread overview
Can't cast to delgate type from void*?
Feb 19, 2007
Rick Mann
Feb 19, 2007
Kirk McDonald
Feb 19, 2007
Rick Mann
February 19, 2007
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

February 19, 2007
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
February 19, 2007
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!