在Elixir中,如何检查Python中的类型:
>>> a = "test"
>>> type(a)
<type 'str'>
>>> b =10
>>> type(b)
<type 'int'>
Run Code Online (Sandbox Code Playgroud)
我在Elixir中读到了类型检查器,例如'is_bitstring','is_float','is_list','is_map'等,但如果您不知道该类型是什么呢?
Fre*_*Dog 159
从elixir 1.2开始,i在iex中有一个命令,它将列出任何Elixir变量的类型和更多.
iex> foo = "a string"
iex> i foo
Term
"a string"
Data type
BitString
Byte size
8
Description
This is a string: a UTF-8 encoded binary. It's printed surrounded by
"double quotes" because all UTF-8 encoded codepoints in it are printable.
Raw representation
<<97, 32, 115, 116, 114, 105, 110, 103>>
Reference modules
String, :binary
Run Code Online (Sandbox Code Playgroud)
如果您查看i命令的代码,您将看到这是通过协议实现的.
https://github.com/elixir-lang/elixir/blob/master/lib/iex/lib/iex/info.ex
如果要为Elixir中的任何数据类型实现函数,那么执行此操作的方法是为希望函数处理的所有数据类型定义协议和协议实现.不幸的是,你不能在守卫中使用协议功能.但是,简单的"类型"协议实现起来非常简单.
wha*_*ide 92
在Elixir/Erlang中没有直接获取变量类型的方法.
您通常想知道变量的类型以便采取相应的行动; 您可以使用这些is_*函数来根据变量的类型进行操作.
了解一些Erlang有一个很好的章节,关于输入Erlang(因此在Elixir中).
使用is_*函数族的最惯用方法可能是在模式匹配中使用它们:
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
Run Code Online (Sandbox Code Playgroud)
ato*_*irk 31
同样出于调试目的,如果你不在iex中,你可以直接调用它:
IEx.Info.info(5)
=> ["Data type": "Integer", "Reference modules": "Integer"]
Run Code Online (Sandbox Code Playgroud)
pop*_*nja 23
另一种方法是使用模式匹配.假设您正在使用Timex,它使用%DateTime{}结构,并且您想要查看元素是否为1.您可以在方法中使用模式匹配找到匹配项.
def is_a_datetime?(%DateTime{}) do
true
end
def is_a_datetime?(_) do
false
end
Run Code Online (Sandbox Code Playgroud)
Dmi*_*try 15
我会把这个放在这里,以便有人希望找出一个真正理智的版本.目前谷歌上没有这个好消息的答案......
defmodule Util do
def typeof(self) do
cond do
is_float(self) -> "float"
is_number(self) -> "number"
is_atom(self) -> "atom"
is_boolean(self) -> "boolean"
is_binary(self) -> "binary"
is_function(self) -> "function"
is_list(self) -> "list"
is_tuple(self) -> "tuple"
_ -> "idunno"
end
end
end
Run Code Online (Sandbox Code Playgroud)
为了完整起见,测试用例:
cases = [
1.337,
1337,
:'1337',
true,
<<1, 3, 3, 7>>,
(fn(x) -> x end),
{1, 3, 3, 7}
]
Enum.each cases, fn(case) ->
IO.puts (inspect case) <> " is a " <> (Util.typeof case)
end
Run Code Online (Sandbox Code Playgroud)
这是一个有协议的解决方案; 我不确定它们是否更快(我当然希望它们不会对所有类型进行循环),但它非常难看(并且非常脆弱;如果它们添加或删除基本类型或重命名,它将会破坏它).
defprotocol Typeable, do: def typeof(self)
defimpl Typeable, for: Atom, do: def typeof(_), do: "Atom"
defimpl Typeable, for: BitString, do: def typeof(_), do: "BitString"
defimpl Typeable, for: Float, do: def typeof(_), do: "Float"
defimpl Typeable, for: Function, do: def typeof(_), do: "Function"
defimpl Typeable, for: Integer, do: def typeof(_), do: "Integer"
defimpl Typeable, for: List, do: def typeof(_), do: "List"
defimpl Typeable, for: Map, do: def typeof(_), do: "Map"
defimpl Typeable, for: PID, do: def typeof(_), do: "PID"
defimpl Typeable, for: Port, do: def typeof(_), do: "Port"
defimpl Typeable, for: Reference, do: def typeof(_), do: "Reference"
defimpl Typeable, for: Tuple, do: def typeof(_), do: "Tuple"
IO.puts Typeable.typeof "Hi"
IO.puts Typeable.typeof :ok
Run Code Online (Sandbox Code Playgroud)
use*_*999 11
我只是粘贴来自https://elixirforum.com/t/just-created-a-typeof-module/2583/5 :) 的代码
defmodule Util do
types = ~w[function nil integer binary bitstring list map float atom tuple pid port reference]
for type <- types do
def typeof(x) when unquote(:"is_#{type}")(x), do: unquote(type)
end
end
Run Code Online (Sandbox Code Playgroud)