Variable Function

BlitzMax Forums/BlitzMax Beginners Area/Variable Function

SpectreNectar(Posted 2010) [#1]
Hi

I'm having some trouble and need your help. I'm making a list of buttons and would like for each of them to call different functions but testing the following didn't work:

Function test(what:Int)
	Print "testing: " + what
End Function

Function eat()
	Print "eating"
End Function

Function sleep()
	Print "zzzzzzZZZZzzZZzzz"
End Function

Local fun:Function = sleep

fun()



So what is a guy to do?
Thx for taking your time helping out.


Brucey(Posted 2010) [#2]

SuperStrict

Function test(what:Int)
	Print "testing: " + what
End Function

Function eat()
	Print "eating"
End Function

Function sleep()
	Print "zzzzzzZZZZzzZZzzz"
End Function

Local fun() = sleep

fun()


Try to use Strict or SuperStrict at the top of your program. It will help you to determine what you've done wrong.


GfK(Posted 2010) [#3]
The buzzphrase you're looking for is "function pointers".

Bit beyond me, so I'll leave it for you to google, or for somebody who knows to explain.


SpectreNectar(Posted 2010) [#4]
Thank you very much :)


Warpy(Posted 2010) [#5]
gfk said what, brucey said how, so I think that leaves me to say why:

It can't just be fun:Function, because two functions can be of different types - one might take more arguments, or arguments of different types, or they might return objects of different types.

For example, this won't work:
Function takeAString(s:String)
End Function

Local fun() = takeAString

but this will
Function takeAString(s:String)
End Function

Local fun(stringArg:String) = takeAString


So, that's why you use the normal function definition syntax to declare a function pointer (function variable).


Czar Flavius(Posted 2010) [#6]
Prepare to be annoyed by the fact a function pointer has to take the same number and type of parameters. Remember that an object parameter can take anything. Use this knowledge wisely.


n-Halbleiter(Posted 2010) [#7]
Objects can take every non-primitive data types.
The Primitive data types are Int,Byte, Long, Short, Float and Double.
String and every Type are non-primitive (or: derived from "Object").


Dreamora(Posted 2010) [#8]
This has nothing to do with annoying czar. Type safety is the base upon which any modern (!C++) OOP language builds upon.

not having type safety in such an environment, especially a managed one, is annoying or precisely inacceptable.


Czar Flavius(Posted 2010) [#9]
That's true!

It's also true that, in a TINY number of circumstances, I've found it much more convenient to do things the naughty way than the proper way :D

But yes, I don't want you to think my code is littered with millions of object parameters everywhere. It would give me a headache too.

In the above situation I would probably make an abstact data holder type, and extend from it data types with different fields depending upon the situation. Then I could use one function pointer which accepts the abstract data type, and a function checks (and reports error if not) it's getting the right data type, and then get its parameters from that.