-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating function and variable docs #1017
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8a97d51
Updating function and variable docs
jonmeow ea1d35e
Worked around backslash
jonmeow 90c0db8
Add a couple README updates
jonmeow 3fde0cf
Address comments
jonmeow 0fa6969
Address comments
jonmeow 44aa290
arg->parameter
jonmeow 2e3dcc4
Addressing comments
jonmeow e295850
Addressing comments
jonmeow 2efde16
Merge branch 'trunk' into var-docs
jonmeow aba3380
Merge branch 'trunk' into var-docs
jonmeow 4394fac
Update docs/design/variables.md
jonmeow 3b4242a
Rephrasing statements.
jonmeow b691a56
Update docs/design/functions.md
jonmeow 2a5c8c4
Update docs/design/functions.md
jonmeow 7efd3cc
Update docs/design/variables.md
jonmeow a019c9c
Remove paragraph
jonmeow b959388
prettier
jonmeow e6d499d
prettier
jonmeow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zygoloid to ensure we're doing a decent job of introducing type inference and not missing any implications or connections to other parts of the language that we should establish... |
||
|
||
[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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps state that switching between
fn F(...)
andfn F(...) -> ()
is a compatible change for all callers? (though the return statements in the function's definition would have to be updated)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest proposing this separately. There are two reasons I could see this not being true:
var x: auto = F()
is either required or disallowed in some cases.In any case, I think this is a non-trivial decision and may be more appropriate for a proposal or its own change (this doc edit has already taken longer than I expected, I'd rather not push it out another couple weeks).