Skip to content

Add ? pseudo-variable #24

@aleksei0807

Description

@aleksei0807

Now we should write tons of boilerplate code like

x, y, z, err := someFunc(a, b, c, d)
if err != nil {
    return err
}

or

x, y, z, err := someFunc(a, b, c, d)
if err != nil {
    log.Printf("some error occurred: %s", err)
}

So we can support ? pseudo-variable, just like _ but with next logic:
if user defines a func, that returns error, the next code will return zero values for any returning variable but last error:

func someFuncWithErrReturn() (string, []byte, error) {
    x, y, z, ? := someFunc()
}

if user defines a func that returns named variables and an error, the next code will return named variables and error:

func someFuncWithErrAndNamedRets() (a string, b int, c []byte, error) {
    a = "a string"
    b = 0
    c = []byte{'0', '1', '2'}
    x, y, z, ? := someFunc()
}

if user defines a func that returns named variables and an error, the next code will return named variables and error, but returned values from someFunc() will overwrite a, b and c:

func someFuncWithErrAndNamedOWRets() (a string, b int, c []byte, error) {
    a = "a string"
    b = 0
    c = []byte{'0', '1', '2'}
    a, b, c, ? = someFunc()
}

if user defines a func that is not returns any error, the error will be printed and function will return zero values for given types:

func someFuncWithoutAnyErr() (string, int, []byte) {
    a, b, c, ? := someFunc()
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions