Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct term for _ in a type hint?

In type hints in Rust it is possible to use partial types in annotations like this:

let myvec: Vec<_> = vec![1, 2, 3];

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.

like image 425
Erik Vesteraas Avatar asked Jul 11 '17 11:07

Erik Vesteraas


People also ask

What is the use of type hints in Python?

Python's type hints provide you with optional static typing to leverage the best of both static and dynamic typing. Besides the str type, you can use other built-in types such as int , float , bool , and bytes for type hintings. To check the syntax for type hints, you need to use a static type checker tool.

What is the type of hint?

Type hinting is a formal solution to statically indicate the type of a value within your Python code. It was specified in PEP 484 and introduced in Python 3.5.

What is list from typing in Python?

List. Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

How do you use a typing tuple?

typing. Tuple is special here in that it lets you specify a specific number of elements expected and the type of each position. Use ellipsis if the length is not set and the type should be repeated: Tuple[float, ...] describes a variable-length tuple with float s. For typing.

What is type hinting in Python?

Type hinting is a formal solution to statically indicate the type of a value within your Python code. It was specified in PEP 484 and introduced in Python 3.5. Here’s an example of adding type information to a function. You annotate the arguments and the return value: The name: str syntax indicates the name argument should be of type str.

How to check for type-hints in Python?

The Type-Hint is completely ignored by the Python interpreter. So, if we run this code again, we still get the same error. So, we have to use a static type checker that analyzes our code and tries to detect if we are violating our Type-Hints or not. The best known type checker is “ mypy “. We can install it by simply using pip .

How do I add type hints to arguments and return values?

Now add type hints by annotating the arguments and the return value as follows: Use normal rules for colons, that is, no space before and one space after a colon ( text: str ). Use spaces around the = sign when combining an argument annotation with a default value ( align: bool = True ).


2 Answers

Seems like the grammar refers to it as an "inferred type". Per the documentation:

Inferred type

Syntax:

InferredType : _

The inferred type asks the compiler to infer the type if possible based on the surrounding information available. It cannot be used in item signatures. It is often used in generic arguments:

let x: Vec<_> = (0..10).collect();
like image 156
solstice333 Avatar answered Oct 06 '22 00:10

solstice333


In the compiler, it seems to be called Infer (in syntax::ast, rustc::hir, and rustc::ty)

I think this naming is somewhat reasonable, because these _s are replaced with fresh (type) inference variables before doing Hindley-Milner-like type inference.

like image 45
Masaki Hara Avatar answered Oct 06 '22 01:10

Masaki Hara