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

proposal: Go 2: Implicit zero-return-value #47008

Closed
leonklingele opened this issue Jul 1, 2021 · 6 comments
Closed

proposal: Go 2: Implicit zero-return-value #47008

leonklingele opened this issue Jul 1, 2021 · 6 comments

Comments

@leonklingele
Copy link
Contributor

leonklingele commented Jul 1, 2021

For example, having a func returning (int, error), it's quite cumbersome to always explicitly return a zero-valued integer in case of an error:

func demo() (int, error) {
    if !someCondition {
        return 0, errors.New("someCondition failed")
    }
    if !someCondition2 {
        return 0, errors.New("someCondition2 failed")
    }

    return 42, nil
}

Using named return values instead makes this a bit easier (but they seem overly complicated and error-prone):

func demo() (i int, err error) {
    if !someCondition {
        err = errors.New("someCondition failed")
        return
    }
    if !someCondition2 {
        err = errors.New("someCondition2 failed")
        return
    }

    return 42, nil
}

What would be more useful, beautiful, is using an underscore (_) to implicitly return the zero-value of a return parameter — a don't-care value:

func demo() (int, error) {
    if !someCondition {
        return _, errors.New("someCondition failed")
    }
    if !someCondition2 {
        return _, errors.New("someCondition2 failed")
    }

    return 42, nil
}

Has this been proposed before?

@gopherbot gopherbot added this to the Proposal milestone Jul 1, 2021
@josharian
Copy link
Contributor

See #21182 (which I quite like, but which doesn't seem to be very popular).

@seankhliao
Copy link
Member

dup of #19642 ?

@DeedleFake
Copy link

DeedleFake commented Jul 1, 2021

@seankhliao

I don't think that it's an exact duplicate. That was allowing it in any RHS usage, while this seems to be only in returns.

@josharian
Copy link
Contributor

In any case I think it’s fair to say that this proposal is reasonably well covered by existing issues. What do you think, @leonklingele ?

@seankhliao
Copy link
Member

#21422 (exactly the same as this one) was closed as a dup of #19642

@leonklingele
Copy link
Contributor Author

Closing as a dup of any of the aforementioned issues.

@golang golang locked and limited conversation to collaborators Jul 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants