简体   繁体   English

Dialyzer 在 Elixir 中的成功输入是什么?

[英]What is Dialyzer’s success typing in Elixir?

Before asking, I have tried to search on Google to understand it, Because Elixir is not widely used, The number of articles is less.在问之前,我已经尝试在谷歌上搜索了解,因为 Elixir 没有被广泛使用,文章数量较少。 The articles which describe Success typing give too abstract information making it difficult for me to understand.描述成功打字的文章提供了过于抽象的信息,使我难以理解。

Can someone please help me to understand it in detail with an example?有人可以通过一个例子帮助我详细理解它吗? if possible, please如果可能,请

Dialyzer and Elixir's use of types and specs is perhaps one of the most confusing things about the language. Dialyzer 和 Elixir 对类型和规范的使用可能是该语言最令人困惑的事情之一。 Elixir is unusual because it is a compiled language that is dynamically typed (!!). Elixir 不寻常,因为它是一种动态类型的编译语言 (!!)。 The "looseness" of the variable typing is by design: having flexibility there is one of the things that helps you do hot-code swapping (which was one of the things for which Erlang was designed).变量类型的“松散性”是设计使然:具有灵活性是可以帮助您进行热代码交换的事情之一(这是 Erlang 的设计目标之一)。

Support for type-specs was added after-the-fact.事后添加了对类型规范的支持。 These declarations are non-essential decorations on top of the functional code -- they don't need to be there, and they don't necessarily need to be totally accurate, which makes them confusing for those of us who have familiarity with strongly-typed languages where any loose interpretation is not tolerated.这些声明是函数式代码之上的非必要装饰——它们不需要在那里,也不一定需要完全准确,这让我们这些非常熟悉的人感到困惑——不允许任何松散解释的类型化语言。

Dialyzer offers a way to evaluate the correctness of the function typing -- it can help identify places where perhaps the listed specs are not accurate. Dialyzer 提供了一种评估函数类型正确性的方法——它可以帮助识别列出的规范可能不准确的地方。 Again, what's confusing here is that this process isn't as strict as we as programmers might expect: sometimes Dialyzer complains about things that are working fine, and sometimes it sees no errors where things are problematic (although its accuracy has improved a lot IMO in the past year alone).同样,这里令人困惑的是,这个过程并不像我们程序员所期望的那样严格:有时 Dialyzer 会抱怨工作正常的事情,有时在有问题的地方它看不到任何错误(尽管它的准确性已经提高了很多 IMO仅在过去一年)。

To make sense of this, I find it helpful to remember the purpose of the specs/annotations: they help communicate to humans what a function accepts and returns (the downstream benefits this provides to the compiler I regard as secondary).为了理解这一点,我发现记住规范/注释的目的是有帮助的:它们有助于向人类传达函数接受和返回的内容(这为我认为次要的编译器提供的下游好处)。 So just remember that if you choose to add success-typing to your functions, go slowly and check with Dialyzer often -- any errors that it comes up with should be seen as a disagreement between what YOU say the code should receive and return vs. what Dialyzer sees the code ACTUALLY doing.所以请记住,如果你选择在你的函数中添加成功类型,请慢慢来并经常检查 Dialyzer——它出现的任何错误都应该被视为你所说的代码应该接收和返回的内容之间的分歧。 Dialyzer 看到代码实际在做什么。 If you don't define any specs, there is no disagreement.如果您不定义任何规范,则没有分歧。 Combing through your code helps you identify all the possible values that a function might return and Dialyzer is a really good instrument for helping you verify that what you say your function does is what it is actually capable of.梳理您的代码可以帮助您识别函数可能返回的所有可能值,而 Dialyzer 是一个非常好的工具,可以帮助您验证您所说的函数所做的是它实际能够做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM