문제

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