CallByName?

BlitzMax Forums/BlitzMax Programming/CallByName?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

(apply F argument-list)

Oh wait, this isn't Lisp. :P Things like optional function arguments and the ability to take any # of arguments to a function sure are nice though. Hmm, how to replicate that in Bmax...

I think your best bet is either a TMap or even better, a THashtable of string keys and function pointers, like Flame mentioned. I'd recommend my Hashtable class here - http://www.blitzmax.com/codearcs/codearcs.php?code=1907

You could still make an easy interface such as "CallByName( "F", your-list-of-arguments-here-as-a-TList) - just implement that as a wrapper function. The pseudo-code for CallByName would look something like this if you always passed your additional arguments in as a list (arguable more flexible than an array in this case), and your functions always had the same return type (int in this example)

Function CallByName( func_name:string , arg_list:tlist )

  '--first create a function pointer, and then lookup the function
  '--in the hash table
  local Func:int( args:tlist ) = Hash_Function_Table.GetEntry( func_name)

  '--call the function and pass it the argument list that was passed to CallByName, the function itself will take apart 
  '--this list however it is appropriate for that function to do so. The function itself should report an error if the 
  '--wrong arguments are passed in (there's no way for "CallByName" to know this as it's just a wrapper.)
 Func( arg_list )

EndFunction