Skip to content
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

Merged
merged 2 commits into from
Nov 27, 2014

Conversation

9rnsr
Copy link
Contributor

@9rnsr 9rnsr commented Sep 13, 2014

Issue 8307 - inconsistent treatment of auto functions
Issue 13336 - auto ref return deduced to be ref despite return value coercion

$(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,
Copy link
Member

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"?

Copy link
Contributor Author

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.

@JakobOvrum
Copy link
Member

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?

@9rnsr
Copy link
Contributor Author

9rnsr commented Sep 14, 2014

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 .

@JakobOvrum
Copy link
Member

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).
Copy link
Member

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));

Copy link
Contributor Author

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.
)
)
Copy link
Member

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.

Copy link
Contributor Author

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() {}

Copy link
Member

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 :).

MartinNowak added a commit that referenced this pull request Nov 27, 2014
Issue 8307 and 13336 - fix documentation around return type inference and ref deduction
@MartinNowak MartinNowak merged commit 88d0c0f into dlang:master Nov 27, 2014
@9rnsr 9rnsr deleted the fix13336 branch January 19, 2015 01:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants