Hi,
just recently i wanted to use the events module to play around with, but since I'm a newbie to nim I ran into difficulties. Considering the following code:
import events
type
StringArgsRef = ref object of EventArgs
args: string
StringArgs = object of EventArgs
args: string
proc handleevent(e: EventArgs) =
echo("handled")
ee = initEventEmitter()
ee.on("TestEvent", handleevent)
var
sArgsRef = StringArgsRef(args: "Hello")
sArgs = StringArgs(args: "Hello")
# compile error type mismatch
ee.emit("TestEvent", sArgsRef)
# works just fine, can even cast the event to StringArgs during handleevent proc
ee.emit("TestEvent", sArgs)
My goal was to inherit from EventArgs to pass different types of arguments with the event. I dont unterstand how the behavior can be explained. Hope someone can help me!The problem is that EventArgs is not defined as a ref object, it expects the struct itself not a pointer, so you have to dereference it with `[]`:
#ee.emit("TestEvent", sArgsRef)
ee.emit("TestEvent", sArgsRef[])