1

I'd like to be able to store a function in a hashtable. I can create a map like:

hash = {}
hash["one"] = def():
   print "one got called"

But I'm not able to call it:

func = hash["one"]
func()

This produces the following error message: It is not possible to invoke an expression on type 'object'. Neither Invoke or Call works.

How can I do it ? From what I'm guessing, the stored function should be cast to something.

Stephan202
  • 59,965
  • 13
  • 127
  • 133
Geo
  • 93,257
  • 117
  • 344
  • 520

2 Answers2

3

You could also use a generic Dictionary to prevent the need to cast to a callable:

import System.Collections.Generic

hash = Dictionary[of string, callable]()
hash["one"] = def():
    print "got one"

fn = hash["one"]
fn()
matt
  • 118
  • 1
  • 7
2

You need to cast to a Callable type:

hash = {}
hash["one"] = def ():
   print "one got called"

func = hash["one"] as callable
func()
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928