首先,我没有看到任何长生不老药类型规范教程,他们在规范中使用变量名编写@spec——相反,我发现的只是类型规范中的类型教程:
@spec calculate(arg1 :: String.t, arg2 :: CustomType.t, arg3 :: String.t)
v.
@spec calculate(String.t, CustomType.t, String.t)
尽管如此,以下为我通过了透析器:
defmodule CustomType do
@type t :: %CustomType{}
defstruct a: nil, b: nil
end
defmodule MyModule do
@spec calculate(arg1::String.t, arg2::CustomType.t, arg3::String.t) :: number
#def calculate(<<arg1::binary>>, %CustomType{} = arg2, <<arg3::binary>>) do
def calculate(arg1, %CustomType{} = arg2, arg3) when is_binary(arg1) and is_binary(arg3) do
calculate(arg1, arg2, [arg3])
end
@spec calculate(String.t, CustomType.t, maybe_improper_list()) :: number
def calculate(<<arg1::binary>>, %CustomType{} = arg2, arg3) when is_list(arg3) do
123
end
end
~/elixir_programs/friends$ mix dialyzer
Compiling 1 file (.ex)
warning: variable "arg1" is unused (if the variable is not meant to be used, prefix it with an underscore)
lib/friends/my_module.ex:17
warning: variable "arg2" is unused (if the variable is not meant to be used, prefix it with an underscore)
lib/friends/my_module.ex:17
Checking PLT...
[:compiler, :connection, :crypto, :db_connection, :decimal, :ecto, :elixir,
:kernel, :logger, :poolboy, :postgrex, :stdlib]
PLT is up to date!
Starting Dialyzer
dialyzer args: [
check_plt: false,
init_plt: '/Users/7stud/elixir_programs/friends/_build/dev/dialyxir_erlang-20.3_elixir-1.8.2_deps-dev.plt',
files_rec: ['/Users/7stud/elixir_programs/friends/_build/dev/lib/friends/ebin'],
warnings: [:unknown]
]
done in 0m1.43s
done (passed successfully)
我会说我找到了这种语法:
@spec calculate(String.t, CustomType.t, String.t)
更容易阅读。
根据Learn You Some Erlang:
is a subtype of the success typing
这警告你,事实上,你的规范对于你的代码应该接受的东西来说太严格了,并告诉你(尽管是间接的)你应该让你的类型规范更宽松,或者在你的函数中更好地验证你的输入和输出反映类型规范。
但是,我无法生成您的透析器输出:
defmodule CustomType do
@type t :: %CustomType{}
defstruct a: nil, b: nil
end
defmodule MyModule do
@spec calculate(arg1 :: String.t,
arg2 :: CustomType.t,
arg3 :: String.t)
:: :ok | :error
def calculate(arg1, %CustomType{} = arg2, arg3)
when is_binary(arg1) and is_binary(arg3) do
calculate(arg1, arg2, [arg3])
end
@spec calculate(arg1 :: String.t,
arg2 :: CustomType.t,
arg3 :: maybe_improper_list())
:: :ok | :error
def calculate(arg1, %CustomType{} = arg2, arg3)
when is_binary(arg1) and is_list(arg3) do
case arg1 do
"hello" -> :ok
"goodbye" -> :error
end
end
end
$ mix dialyzer
Compiling 1 file (.ex)
warning: variable "arg2" is unused (if the variable is not meant to be used, prefix it with an underscore)
lib/friends/my_module.ex:19
Checking PLT...
[:compiler, :connection, :crypto, :db_connection, :decimal, :ecto, :elixir,
:kernel, :logger, :poolboy, :postgrex, :stdlib]
PLT is up to date!
Starting Dialyzer
dialyzer args: [
check_plt: false,
init_plt: '/Users/7stud/elixir_programs/friends/_build/dev/dialyxir_erlang-20.3_elixir-1.8.2_deps-dev.plt',
files_rec: ['/Users/7stud/elixir_programs/friends/_build/dev/lib/friends/ebin'],
warnings: [:unknown]
]
done in 0m1.46s
done (passed successfully)
因此,您需要发布一个最小的示例来重现您的透析器输出。我会注意到 arg3 在你的第一个子句中必须是一个二进制文件,所以当你调用calculate(arg1, arg2, [arg3])
第一个子句的主体时,参数[arg3]
永远不会是一个不正确的列表,所以你可以将该规范收紧到:list(binary)
对于第二个子句。
=============
这是我放在一起的代码:
defmodule CustomType do
@type t :: %CustomType {
prop1: String.t(),
prop2: integer(),
prop3: String.t(),
prop4: boolean()
}
defstruct prop1: nil, prop2: nil, prop3: nil, prop4: nil
end
defmodule MyModule do
@spec calculate(arg1 :: String.t,
arg2 :: CustomType.t,
arg3 :: String.t)
:: :ok | :error
def calculate(arg1, %CustomType{} = arg2, arg3)
when is_binary(arg1) and is_binary(arg3) do
calculate(arg1, arg2, [arg3])
end
@spec calculate(arg1 :: String.t,
arg2 :: CustomType.t,
arg3 :: maybe_improper_list)
:: :ok | :error
def calculate(arg1, %CustomType{prop1: val, prop2: val2}, arg3)
when is_binary(arg1) and is_binary(val) and is_integer(val2) and is_list(arg3) do
case arg1 do
"hello" -> :ok
"goodbye" -> :error
end
end
end
运行透析器:
~/elixir_programs/friends$ mix dialyzer
Checking PLT... [:artificery,
:compiler, :connection, :crypto, :db_connection, :decimal, :distillery,
:ecto, :elixir, :kernel, :logger, :poolboy, :postgrex, :runtime_tools,
:stdlib] Finding suitable PLTs Looking up modules in dialyxir_erlang-
20.3_elixir-1.8.2_deps-dev.plt Finding applications for dialyxir_erlang-
20.3_elixir-1.8.2_deps-dev.plt Finding modules for dialyxir_erlang-
20.3_elixir-1.8.2_deps-dev.plt Checking 718 modules in dialyxir_erlang-
20.3_elixir-1.8.2_deps-dev.plt Adding 56 modules to dialyxir_erlang-
20.3_elixir-1.8.2_deps-dev.plt Starting Dialyzer dialyzer args: [
check_plt: false,
init_plt: '/Users/7stud/elixir_programs/friends/_build/dev/dialyxir_erlang-
20.3_elixir-1.8.2_deps-dev.plt', files_rec:
['/Users/7stud/elixir_programs/friends/_build/dev/lib/friends/ebin'],
warnings: [:unknown] ] done in 0m1.26s done (passed successfully)
在 mix.exs 中有以下内容:
def project do
[
app: :friends,
version: "0.1.0",
elixir: "~> 1.6",
start_permanent: Mix.env() == :prod,
deps: deps(),
dialyzer: [
flags: ~w[underspecs
overspecs
race_conditions
error_handling
unmatched_returns]a
]
]
end
这是输出:
~/elixir_programs/friends$ mix dialyzer
Checking PLT...
[:artificery, :compiler, :connection, :crypto, :db_connection, :decimal,
:distillery, :ecto, :elixir, :kernel, :logger, :poolboy, :postgrex,
:runtime_tools, :stdlib]
PLT is up to date!
Starting Dialyzer
dialyzer args: [
check_plt: false,
init_plt: '/Users/7stud/elixir_programs/friends/_build/dev/dialyxir_erlang-20.3_elixir-1.8.2_deps-dev.plt',
files_rec: ['/Users/7stud/elixir_programs/friends/_build/dev/lib/friends/ebin'],
warnings: [:underspecs, :overspecs, :race_conditions, :error_handling,
:unmatched_returns, :unknown]
]
done in 0m1.38s
done (passed successfully)