3 ms·
> Or create wrappers to make your types chainable It can't always be done. A generic wrapper only works for functions that return values. It would be nice to h
by vorg 11y ago
> Or create wrappers to make your types chainable
It can't always be done. A generic wrapper only works for functions that return values. It would be nice to have a wrapper that can take any function, e.g. foo in:
package main
import "fmt"
func foo(a ...interface{})interface{}{
if len(a) == 0 {
return nil
} else if len(a) == 1 {
return a[0]
} else {
return a
}
}
func barbar() (a, b int) { return 1, 2 }
func bar() (a int) { return 1 }
func baz() { return }
func main() {
fmt.Println( foo(barbar()) ) // prints: [1, 2]
fmt.Println( foo(bar()) ) // prints: 1
fmt.Println( foo(baz()) ) // compile error: baz() used as value
}
Instead of the compile error, it would be nice if foo accepted baz() as a argument, which would read the array length as 0 and here return nil.