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

Allow variable redeclaration from multiple return function call #329

Closed
marekmaskarinec opened this issue Dec 25, 2023 · 1 comment
Closed
Labels
enhancement New feature or request

Comments

@marekmaskarinec
Copy link
Contributor

Example:

a, err := something()
if err != null ...

b, err := somethingElse()
if err != null ...

Go allows this behavior:

Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original.

@vtereshkov vtereshkov added the enhancement New feature or request label Dec 25, 2023
@vtereshkov
Copy link
Owner

Done. This example is now valid:

type Err = any

fn something(): (int, ^Err) {
        return 42, new(Err, []int{4, 5})
}

fn somethingElse(): (str, ^Err) {
        return "Hello", new(Err, "World")
}

fn main() {
        a, err := something()
        if err != null {
                printf("%v %v\n", a, err^)  // 42 [4 5]
        }

        b, err := somethingElse()
        if err != null {
                printf("%v %v\n", b, err^)  // "Hello" "World"
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants