goodlang / good

The Good Programming Language
BSD 3-Clause "New" or "Revised" License
29 stars 3 forks source link

Add `?` pseudo-variable #24

Open aleksei0807 opened 7 years ago

aleksei0807 commented 7 years ago

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