D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 13986 - auto return for some recursive functions
Summary: auto return for some recursive functions
Status: RESOLVED DUPLICATE of issue 5288
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-01-15 17:57 UTC by bearophile_hugs
Modified: 2022-03-21 08:25 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2015-01-15 17:57:55 UTC
According to Wikipedia:
http://en.wikipedia.org/wiki/C%2B%2B14#Function_return_type_deduction

C++14 is able to compile code like this:



auto correct(int i) {
    if (i == 1)
        return i;
    else
        return correct(i - 1) + i;
}


But not like this:

auto correct(int i) {
    if (i != 1)
        return correct(i - 1) + i;
    else
        return i;
}


D isn't able to compile both. Is it possible and a good idea to allow the first function in D too?

The D spec says:

"If multiple return expressions are used in the function's implementation, then they must all deduce the same type."
Comment 1 Mathias LANG 2022-03-21 08:25:43 UTC

*** This issue has been marked as a duplicate of issue 5288 ***