在Julia中向Dict添加和调用函数

在Julia中向Dict添加和调用函数,julia,Julia,假设我有一个口述 acts = Dict{String, Function}() 我有一个函数foo() 首先,如何在acts Dict中存储“bar”=>foo() 其次,如何从acts调用bar并运行它?应该很简单: julia> acts = Dict{String, Function}() Dict{String,Function} with 0 entries julia> function foo(arg1, arg2) @info arg1

假设我有一个口述

acts = Dict{String, Function}()
我有一个函数foo()

首先,如何在acts Dict中存储“bar”=>foo()


其次,如何从acts调用bar并运行它?

应该很简单:

julia> acts = Dict{String, Function}()
Dict{String,Function} with 0 entries

julia> function foo(arg1, arg2)
           @info arg1
           @info arg2
       end 
foo (generic function with 1 method)

# add function foo to dict acts with key "bar"
julia> acts["bar"] = foo
foo (generic function with 1 method)

# check that it's there
julia> acts
Dict{String,Function} with 1 entry:
  "bar" => foo

# call the foo function from the dict by using its key
julia> acts["bar"]("hi", 2)
[ Info: hi
[ Info: 2
julia> acts = Dict{String, Function}()
Dict{String,Function} with 0 entries

julia> function foo(arg1, arg2)
           @info arg1
           @info arg2
       end 
foo (generic function with 1 method)

# add function foo to dict acts with key "bar"
julia> acts["bar"] = foo
foo (generic function with 1 method)

# check that it's there
julia> acts
Dict{String,Function} with 1 entry:
  "bar" => foo

# call the foo function from the dict by using its key
julia> acts["bar"]("hi", 2)
[ Info: hi
[ Info: 2