Type inference
Table of contents
Overview
Type inference occurs in Carbon when the auto
keyword is used. This may occur in variable declarations or function declarations.
At present, type inference is very simple: given the expression which generates the value to be used for type inference, the inferred type is the precise type of that expression. For example, the inferred type for auto
in fn Foo(x: i64) -> auto { return x; }
is i64
.
Type inference is currently supported for function return types and declared variable types.
Open questions
Inferring a variable type from literals
Using the type on the right side for var y: auto = 1
currently results in a constant IntLiteral(1)
value, whereas most languages would suggest a variable integer type, such as i64
. Carbon might also make it an error. Although type inference currently only addresses auto
for variables and function return types, this is something that will be considered as part of type inference in general, because it also affects checked generics, templates, lambdas, and return types.
Alternatives considered
References
- Proposal #851: auto keyword for vars