good icon indicating copy to clipboard operation
good copied to clipboard

Add `?` pseudo-variable

Open aleksei0807 opened this issue 8 years ago • 0 comments

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

aleksei0807 avatar Feb 10 '17 04:02 aleksei0807