A workaround for something like objects
-
-
All, this is not a workaround for objects. It doesn't do what Peter thinks it does. But rather than just delete it, let me instead explain exactly what it DOES do because it will be a useful explanation for people.
Code is shortened as I'm feeling somewhat lazy but it still demonstrates the topic
function test() print("Hello World", chr(10)) update() sleep(2) return void my_objects = [ .func = test() ] my_objects.func
So let's look at the code. Ignore what's in the function, it doesn't matter, the key thing is that if you call it with
1
it prints something.At first glance it looks like the line
myObj = [ .func = test(1) ]
is storing a reference to the function which is then being called when you subsequently "call" it usingmyObj.func
. But what is actually happening is that during the struct initialisation the method is called, it prints Hello World and returns void. At this pointmyObj.func
equals void.Subsequently the statement
myObj.func
quite literally means and does nothing. In fact, you could be asking for trouble and enticing a crash. Hopefully this gets comepletely ignored by Fuze but I wouldn't chance it myself.You can prove this by repeating the line 3 times. You'll still only get Hello World printed once.
For the record, storing function pointers in struct members is really a pretty advanced thing for a language to implement and it is not available in Fuze. No amount of wishing it will make it so I'm afraid.
I'll leave this here for the explanation.