How do you check for the type of variable in Elixir

前端 未结 8 1276
走了就别回头了
走了就别回头了 2021-01-30 15:25

In Elixir how do you check for type such as in Python:

>>> a = \"test\"
>>> type(a)

>>> b =10
>>> type(b         


        
8条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 15:50

    There's no direct way to get the type of a variable in Elixir/Erlang.

    You usually want to know the type of a variable in order to act accordingly; you can use the is_* functions in order to act based on the type of a variable.

    Learn You Some Erlang has a nice chapter about typing in Erlang (and thus in Elixir).

    The most idiomatic way to use the is_* family of functions would probably be to use them in pattern matches:

    def my_fun(arg) when is_map(arg), do: ...
    def my_fun(arg) when is_list(arg), do: ...
    def my_fun(arg) when is_integer(arg), do: ...
    # ...and so on
    

提交回复
热议问题