How do you check for the type of variable in Elixir

前端 未结 8 1277
走了就别回头了
走了就别回头了 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 16:05

    Another approach is to use pattern matching. Say you're using Timex, which uses a %DateTime{} struct, and you want to see if an element is one. You can find a match using pattern matching in the method.

    def is_a_datetime?(%DateTime{}) do
      true
    end
    
    def is_a_datetime?(_) do
      false
    end
    

提交回复
热议问题