CallByName?

BlitzMax Forums/BlitzMax Programming/CallByName?

Blueapples(Posted 2007) [#1]
Is there anything like CallByName in VB?

http://msdn2.microsoft.com/en-us/library/chsc1tx6(vs.80).aspx

I would like to be able to pass the name of a function to call and an array of arguments. Ability to call on a object isn't as important, I can wrap everything in procedural calls if it's just possible to call a global function by it's name.


FlameDuck(Posted 2007) [#2]
Couldn't you just load a TMap with String keys, and function pointer values?


Blueapples(Posted 2007) [#3]
That would take care of the name problem, sort of. It's a workable solution but not really ideal. I just wanted to know if there was something I was missing. The problem with using a TMap (which is basically what I did in a previous implementation of this same thing in a different language) is that it still doesn't let me apply an array of values to a parameter list.

i.e., a function like F(a,b,c:int) called with CallByName("F", [1,2,3]). Oh well. I can work around it but it would make the design of the stuff I'm working on a lot cleaner.


FlameDuck(Posted 2007) [#4]
is that it still doesn't let me apply an array of values to a parameter list.
Why not?


SculptureOfSoul(Posted 2007) [#5]
(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



Blueapples(Posted 2007) [#6]
It's unfortunate that it's not built in to BMax.

Basically, Soul, I am writing a LISP-like interpreter and I wanted to make it easy for people to use their existing set of BMax functions from within it. I have a runtime object that contains a stack (implemented as a TList) so I will use that for wrapper functions. It's just unfortunate that in order to use an BMax function, I (or the user) will have to write wrappers that peel things off the stack and pass them in to the real function.