I want to choose the routine of the program in run time. I couldn't find any function pointer type (or a reference to a method) in Elixir.

defmodule MyModule do
    def method do
        choice()
    end
end

I want to give a method to choose at run time. How to do this?

有帮助吗?

解决方案

You can send in a function as parameter and then call that function (with .(...)) like this:

iex> defmodule MyModule do
   >     def my_method(choice) do
   >         choice.("hello")
   >     end
   >  end

and then call it by using & before the module name like:

iex> MyModule.my_method(&String.upcase/1)
"HELLO"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top