-
-
Notifications
You must be signed in to change notification settings - Fork 367
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
Issue 8307 and 13336 - fix documentation around return type inference and ref deduction #651
Conversation
$(P If there are multiple $(I ReturnStatement)s, the types | ||
of them must match exactly. If there are no $(I ReturnStatement)s, | ||
$(P If there are multiple $(I ReturnStatement)s, the types | ||
of them must have common. If there are no $(I ReturnStatement)s, |
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.
"have common" -> "must be implicitly convertible to a common 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.
Thanks for the better wording. Fixed.
Wow, since when did return type inference use the common type of all return statements instead of just using the type of the first return statement? I remember Walter repeatedly saying it's the latter, for ease of implementation purposes. Was the implementation changed at some point? |
It's from 2.057. The first compiler code change about that was in https://github.com/D-Programming-Language/dmd/pull/476/files#diff-0489411409cd2934730e88bf77677907L3589 . |
Thanks! |
$(P If there are multiple $(I ReturnStatement)s, the types | ||
of them must be implicitly convertible to a common type. | ||
If there are no $(I ReturnStatement)s, the return type is inferred | ||
to be $(D_KEYWORD void). |
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.
This also happens if the return statement expression for all return statements is void
:
void foo() {}
auto bar() { return foo(); }
static assert(is(typeof(bar()) == void));
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.
Yes. In that case, the common type is deduced tovoid
. That's intended behavior.
If it does not already have a storage class, use the | ||
$(D_KEYWORD auto) storage class. | ||
) | ||
) |
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.
What could that paragraph possibly mean?
It was added with d0a87f8.
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.
This paragraph means that all use of "storage classes" (syntactically equivalent with "attributes") can make the function an auto-function.
const foo() {} // auto-function
pure nothrow foo() {} // auto-function
If there's no storage class, you should use auto
to declare auto-function.
auto foo() {}
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.
Ah right, that's how to explain something. First tell people about exceptional cases, then declare the normal usage as exception of the exception :).
Issue 8307 and 13336 - fix documentation around return type inference and ref deduction
Issue 8307 - inconsistent treatment of auto functions
Issue 13336 - auto ref return deduced to be ref despite return value coercion