-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating function and variable docs (#1017)
I was doing this for #851 initially, but I think #438 and #826 hadn't made it in (possibly intentionally due to #851? I don't recall). Co-authored-by: Chandler Carruth <[email protected]> Co-authored-by: Richard Smith <[email protected]>
- Loading branch information
1 parent
6c3e1f6
commit a2728f8
Showing
4 changed files
with
212 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Type inference | ||
|
||
<!-- | ||
Part of the Carbon Language project, under the Apache License v2.0 with LLVM | ||
Exceptions. See /LICENSE for license information. | ||
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
--> | ||
|
||
<!-- toc --> | ||
|
||
## Table of contents | ||
|
||
- [Overview](#overview) | ||
- [Open questions](#open-questions) | ||
- [Inferring a variable type from literals](#inferring-a-variable-type-from-literals) | ||
- [Alternatives considered](#alternatives-considered) | ||
- [References](#references) | ||
|
||
<!-- tocstop --> | ||
|
||
## Overview | ||
|
||
[Type inference](https://en.wikipedia.org/wiki/Type_inference) occurs in Carbon | ||
when the `auto` keyword is used. This may occur in | ||
[variable declarations](variables.md) or [function declarations](functions.md). | ||
|
||
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](functions.md) | ||
and [declared variable types](variables.md). | ||
|
||
## 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 generics, templates, lambdas, and return types. | ||
|
||
## Alternatives considered | ||
|
||
- [Use `_` instead of `auto`](/proposals/p0851.md#use-_-instead-of-auto) | ||
|
||
## References | ||
|
||
- Proposal | ||
[#851: auto keyword for vars](https://github.com/carbon-language/carbon-lang/pull/851) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters