简体   繁体   English

用管道运算符分隔括号中的类型的Elixir类型规范的含义是什么?

[英]What is the meaning of an Elixir typespec with types in parenthesis separated by the pipe operator?

I'm porting an Elixir library to Elm that uses type specs heavily but I'm having trouble finding documentation on some of the syntax used. 我正在将Elixir库移植到大量使用类型规范的Elm中,但是在查找有关某些语法的文档时遇到了麻烦。

What is the following type expression expressing? 以下类型表达式表示什么?

@type identifier :: (String.t | MyCustomTypeA.t | MyCustomTypeB.t)

Initially I modeled it as a tuple but now that I'm looking at it's usage it looks as though it may be a Discriminated Union. 最初,我将其建模为元组,但现在我正在研究它的用法,好像它可能是一个有区别的联盟。 The problem with this assumption though is that I don't see any documentation for support of such things here ( http://elixir-lang.github.io/getting-started/typespecs-and-behaviours.html ). 但是,这种假设的问题在于,我在这里看不到任何支持此类事情的文档( http://elixir-lang.github.io/getting-started/typespecs-and-behaviours.html )。

You are correct in that it's a Discriminated Union. 您是对的,因为它是歧视联盟。 Neither the Elixir nor Erlang docs call it out directly but it can be inferred from the more in-depth docs ( https://hexdocs.pm/elixir/typespecs.html ) Elixir和Erlang文档都没有直接指出来,但是可以从更深入的文档( https://hexdocs.pm/elixir/typespecs.html )中推断出来。

The only real call-out to this is the line 唯一真正的标注是生产线

All other types are built out of unions of predefined types. 所有其他类型都是根据预定义类型的并集构建的。

In your example, the parenthesis are not required. 在您的示例中,不需要括号。 You could also write it as 你也可以写成

@type identifier :: String.t | MyCustomTypeA.t | MyCustomTypeB.t

and it means that identifier can be either a String.t , a MyCustomTypeA.t , or a MyCustomTypeB.t 这意味着identifier可以是String.tMyCustomTypeA.tMyCustomTypeB.t

Elixir inherits this from Erlang and it's well explained in Learn you some Erlang for great good Elixir从Erlang继承了这一点,并在“ 学到一些Erlang带来的好处”中对此进行了很好的解释。

(please remember that while Elixir builds on Erlang the syntax differs a bit) (请记住,尽管Elixir基于Erlang构建,但语法略有不同)

Erlang has union types, which allow you to describe a type that has two atoms in it, and built-in types, which are pre-defined types, not necessarily possible to build by hand, and they're generally useful. Erlang具有并集类型,它允许您描述其中具有两个原子的类型,以及内置类型,它们是预定义的类型,不一定可以手动构建,并且它们通常很有用。

[...SNIP...] [...略...]

The notation to represent the union of types is the pipe (|). 表示类型并集的表示法是竖线(|)。 Basically, this lets us say that a given type TypeName is represented as the union of Type1 | 基本上,这可以说给定类型TypeName表示为Type1 | Union的并集。 Type2 | Type2 | ... | ... | TypeN. typen的。 As such, the number() type, which includes integers and floating point values, could be represented as integer() | 这样,包括整数和浮点值的number()类型可以表示为integer()| float(). 浮动()。 A boolean value could be defined as 'true' | 布尔值可以定义为'true'| 'false'. '假'。 It is also possible to define types where only one other type is used. 也可以定义仅使用一种其他类型的类型。 Although it looks like a union type, it is in fact an alias. 尽管它看起来像联合类型,但实际上是别名。

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

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