In Elixir how do you check for type such as in Python:
>>> a = \"test\"
>>> type(a)
>>> b =10
>>> type(b
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
I came across a situation need to check the parameter need to be certain type. Maybe can active a better way.
Like this:
@required [{"body", "binary"},{"fee", "integer"}, ...]
defp match_desire?({value, type}) do
apply(Kernel, :"is_#{type}", [value])
end
Usage:
Enum.map(@required, &(match_desire?/1))