类型提示中_的正确术语是什么? [英] What is the correct term for _ in a type hint?
问题描述
在Rust的类型提示中,可以在注释中使用部分类型:
In type hints in Rust it is possible to use partial types in annotations like this:
let myvec: Vec<_> = vec![1, 2, 3];
部分类型注释中下划线的正确术语是什么?我对Rust术语以及学术类型理论术语都感兴趣.
What is the correct terminology for the underscore in the partial type annotation? I'm interested in both the Rust terminology as well as more academic type theory terminology.
推荐答案
经过一番挖掘,看来Vec<_>
始终被称为部分类型(因此在let x: Vec<_>
中,我们有部分类型注释,而Fn(String) -> _
会是部分类型签名),但是在这种情况下,_
被不同地称为类型通配符或类型占位符,并且类型语法中的_
可以被理解为推断此类型"的标记(当时下面提到的PR,TyInfer
在编译器内部).
After some digging it seems that Vec<_>
is consistently called a partial type (so in let x: Vec<_>
we have a partial type annotation, while Fn(String) -> _
would be a partial type signature) but the _
in this context is varyingly called either a type wildcard or a type placeholder, and _
in the type grammar can be read as the token for "infer this type" (at the time of the PR mentioned below, TyInfer
internally in the compiler).
一些有趣的读物:
- Haskell中的部分类型签名
- 将
_
添加到Rust类型语法的拉取请求 - 混合参数列表-Niko Matsakis的博客文章中,他建议将
_
作为未指定寿命或类型的符号引入"
- Partial type signatures in Haskell
- The pull request which added
_
to the Rust type grammar - Intermingled parameter lists - Niko Matsakis' blog post in which he proposes to "Introduce
_
as a notation for an unspecified lifetime or type"
PR的有趣细节:
let x: _ = 5;
let x = 5;
上面的两行是等效的,并且都被解析为类型为TyInfer
的变量x
.
The two lines above are equivalent, and both parsed as variable x
with type TyInfer
.
这篇关于类型提示中_的正确术语是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!