-
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
Function return type inference #826
Function return type inference #826
Conversation
Co-authored-by: Geoff Romer <[email protected]>
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.
Generally makes sense. Would like to specifically know if @zygoloid has anything we should ensure to avoid here and whether this seems reasonable to go in at this stage.
proposals/p0826.md
Outdated
For example, this is valid because `CallAdd` can see the `Add` definition: | ||
|
||
``` | ||
fn Add(x: i32, y: i32) -> auto; | ||
|
||
fn Add(x: i32, y: i32) -> auto { return x + y; } |
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 don't think we should even allow this. While the caller is fine, we can't type check the first declaration. I'd suggest requiring a definition to accompany any declaration with auto
.
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 think disallowing this makes sense. A forward declaration involving auto
is usually not useful -- you can't actually call it, so it doesn't break any cycles, and you can't use it to move the implementation out of an api
file. Perhaps it's useful to allow you to declare a member function inside a class and then define it outside, though I'm not sure how valuable that is. If we're deducing a return type, I'd expect the function body to be small.
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.
Updated proposal and added alternative corresponding.
proposals/p0826.md
Outdated
For example, this is valid because `CallAdd` can see the `Add` definition: | ||
|
||
``` | ||
fn Add(x: i32, y: i32) -> auto; | ||
|
||
fn Add(x: i32, y: i32) -> auto { return x + y; } |
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 think disallowing this makes sense. A forward declaration involving auto
is usually not useful -- you can't actually call it, so it doesn't break any cycles, and you can't use it to move the implementation out of an api
file. Perhaps it's useful to allow you to declare a member function inside a class and then define it outside, though I'm not sure how valuable that is. If we're deducing a return type, I'd expect the function body to be small.
proposals/p0826.md
Outdated
|
||
## Proposal | ||
|
||
Support automatic type inference in Carbon with `auto`, as in: |
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.
FWIW, I think this is our first allowance for the use of auto
anywhere. I don't think that's a problem in and of itself, but it may be worth saying that auto
will be a keyword.
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.
Added notes to the background, and a very brief note here. (TBH, I had thought #339 included it, but obviously not -- maybe it's worth a proposal for a starting point)
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.
Addressing comments
proposals/p0826.md
Outdated
|
||
## Proposal | ||
|
||
Support automatic type inference in Carbon with `auto`, as in: |
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.
Added notes to the background, and a very brief note here. (TBH, I had thought #339 included it, but obviously not -- maybe it's worth a proposal for a starting point)
proposals/p0826.md
Outdated
For example, this is valid because `CallAdd` can see the `Add` definition: | ||
|
||
``` | ||
fn Add(x: i32, y: i32) -> auto; | ||
|
||
fn Add(x: i32, y: i32) -> auto { return x + y; } |
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.
Updated proposal and added alternative corresponding.
I think this proposal has bounded the scope enough to avoid most problems here: we require exactly one I think there are two more issues from C++ that we should consider, though I think at least one of them is out of scope for now:
In terms of what to do with the above in this document, I think adding something saying we don't permit recursive calls in functions with a deduced return type at least for now (and that this intentionally diverges from C++) would be sufficient. |
Strongly agree with just rejecting all recursion here. Seems much simpler and more obvious.
Yeah, I agree this is a bit out of scope for now. I'd revisit this (and any other |
https://github.com/carbon-language/carbon-lang/pull/826/files/be3f11ee0fb54e3bf336a64ef4f10c8967de3ba6..aa305e36838ef0f62fa2a8b985a17073f6995231 should address both points about direct recursion and |
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.
Some minor wording suggestions below to just make things read a bit easier for me. Generally, LGTM. Feel free to merge with these or similar. Also happy to look if the wording suggestions aren't making sense or are changing anything.
proposals/p0826.md
Outdated
However, this would be invalid because `CallAdd` can only see the `Add` | ||
declaration (even though the definition may be in the same file), and it would | ||
need the definition to determine the return type: |
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.
Just trying to make "this" a bit less ambiguous:
However, this would be invalid because `CallAdd` can only see the `Add` | |
declaration (even though the definition may be in the same file), and it would | |
need the definition to determine the return type: | |
However, the example below would be invalid because `CallAdd` can only see the `Add` | |
declaration (even though the definition may be in the same file), and it would | |
need the definition to determine the return type: |
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.
FYI re: below, more specific words are preferred: https://developers.google.com/style/word-list#letter-b
I'll probably do something like "following example"
Co-authored-by: Chandler Carruth <[email protected]>
Co-authored-by: Chandler Carruth <[email protected]>
…ow/carbon-lang into proposal-fn-returning-auto
This implements #826, I think covering everything important there. Regarding ReturnTypeContext, I broke that out because it started feeling like a significant number of args to be passing around, and I think this makes the association inside type checking clearer. Co-authored-by: Geoff Romer <[email protected]>
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]>
Allow `auto` as a return type for functions. Only support functions with one `return` statement for now (open question). This explicitly suggests removing the executable semantics `fn name(args) => expression` syntax, and is motivated by reconciling executable semantics with approved Carbon state. [example](https://github.com/carbon-language/carbon-lang/blob/3d1716f6c692a840d8b4b513ddfb8119432a5150/executable_semantics/testdata/fun_named_params.carbon) This aspect is a decision that may be affected by lambda syntax, but we might also choose to keep lambda syntax and function syntax separate -- I don't think there's enough benefit to providing the alternate function syntax right now. Co-authored-by: Geoff Romer <[email protected]> Co-authored-by: Chandler Carruth <[email protected]>
This implements #826, I think covering everything important there. Regarding ReturnTypeContext, I broke that out because it started feeling like a significant number of args to be passing around, and I think this makes the association inside type checking clearer. Co-authored-by: Geoff Romer <[email protected]>
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]>
Allow
auto
as a return type for functions. Only support functions with onereturn
statement for now (open question).This explicitly suggests removing the executable semantics
fn name(args) => expression
syntax, and is motivated by reconciling executable semantics with approved Carbon state. example This aspect is a decision that may be affected by lambda syntax, but we might also choose to keep lambda syntax and function syntax separate -- I don't think there's enough benefit to providing the alternate function syntax right now.