[Closed] Weird struct + callback behaviour
The following code produces an “unknown system exception”.
Does anyone has any idea why?
struct dostuff (
fn updateS = print "ites",
fn toggleS tstate = (
if tstate then
registerRedrawViewsCallback dostuff.updateS
else
unRegisterRedrawViewsCallback dostuff.updateS
)
)
dostuff = dostuff()
dostuff.toggleS true
If I run the register and unregister outside the struct they work fine… so a callback does accept struct functions for registering. But somehow I cannot register them from within the struct!? And I would really really like to keep that function in the struct.
Any clues?!
-Johan
call your register/unregister funcs without “dostuff.”-prefix
you are already inside this dostuff-struct
if you call it from outside you need of course
guruware
fn toggleS tstate = (
if tstate then
registerRedrawViewsCallback updateS
else
unRegisterRedrawViewsCallback updateS
)
I see that but in reality i instantiate the struct with a different name, so my callback needs to know from wich struct it was instanciated. Hence the direct call from within the structure.
I feel it’s a scope issue as well, but I just don’t get it… because from the outside I can call the the instanced struct and it works… but I can’t seem to call it from inside… but(!) I do this all the time for other types of functions in an instanciated struct… I think PEN does as well…
-Johan
This should work as I do it all the time. Let me have a kick at it and see what is happening.
That looks like it works.
struct dostuff
(
fn updateS = (print "Working"),
fn toggleS tstate =
(
if tstate then
registerRedrawViewsCallback updateS
else
unRegisterRedrawViewsCallback updateS
)
)
dostuff = dostuff()
dostuff.toggleS true
Ok… boi I feel stupid…
You can’t instanciate a struct like:
newStruct = oldStruct()
That’s calling a function
I needed to:
newStruct = oldStruct
Funny thing, because not to long ago I pointed the same thing out to, I thought it was Jeff Hanna, who was making the same mistake…
Sorry for taking up your time, because of a typo…
But thanx!
-Johan
so does making an instance of the struct to the same name as the struct still makes it an instance?
Reason i ask is that when i define a struct I make the instance a global and set the struct to that variable. Inside the struct i call the variable name. I have done this because in the past i have received a “needs an instance” error from calling functions within the struct definition.
also, is there a reason you initialise the struct to a variable with the function call brackets? i.e. – dostuff = dostuff()
i’ve just made the struct and declared the members to a global variable in the past. i’m probably doing it wrong, though! so any pointers about struct housekeeping would be welocme!
ok, in the time it took to write my reply about the function call brackets , it was answered!
jup… I have not had any problem making a global variable holding the struct and making it the same name as the actual struct… but for clarity I use a different name… like:
global JHN_theStruct
theStruct value1, value2, fn pprint value = print value, fn doprint = JHN_theStruct.pprint JHN_theStruct.value1
JHN_theStruct = theStruct –note no brackets here (boi i’m I wrong… read my last post)