Добавить и вызвать функцию в и из Dict в Джулии - PullRequest
2 голосов
/ 24 апреля 2020

предположим, у меня есть Dict

acts = Dict{String, Function}()

и у меня есть функция foo ()

function foo(arg1, arg2)
  @info arg1
  @info arg2
end

Во-первых, как я могу сохранить "bar" => foo () в действиях Dict?

Во-вторых, как я могу вызвать бар из актов и запустить его?

1 Ответ

3 голосов
/ 24 апреля 2020

Должно быть простым:

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
...