digitalmars.D.learn - Can't cast to delgate type from void*?
- Rick Mann (19/19) Feb 18 2007 I'm trying to use delegates with Mac OS X's carbon events. When you regi...
- Kirk McDonald (10/36) Feb 18 2007 Delegates are 8 bytes. They consist of a function pointer and a context
- Rick Mann (2/6) Feb 18 2007 Yeah, I finally figured this out. I thought the delegate was actually a ...
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
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, RickDelegates 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
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