3 ms·
Can you explain how tuples are significantly different from multiple returns in real programs? You can do func foo() (string, int, bool) { return
by NateDad 12y ago
Can you explain how tuples are significantly different from multiple returns in real programs? You can do
func foo() (string, int, bool) {
return "one", 2, true
}
func bar(s string, i int, b bool) {}
bar(foo())
- meowface 12y agoWhen dealing with function calls, they're roughly the same. But how about things like: g = some_input() # A list with 3 elements a, b, c = g Or in Python 3+: lst = [1, 2, 3, 4] head, *tail = lst # 1, [2, 3, 4] *init, last = lst # [1, 2, 3], 4 a, *b, c = lst # 1, [2, 3], 4
- NateDad 12y agoYou can do that in go with just a few more characters: g := some_input() // A list with 3 elements a, b, c := g[0], g[1], g[2] I'll grant you, having to specify the indices is slightly more verbose, but it's also a lot more clear... because what happens when g has more or less than 3 elements? In Go it's clear, more is ok, less will get you a panic. lst := []int{1, 2, 3, 4} head, tail := lst[0], lst[1:] init, last := lst[:len(lst)-1], lst[len(lst)-1] a, b, c := lst[0], lst[1:3], lst[4] Again, I don't think the magic unpacking is really helping that much... doing it the same way you'd do anything else in Go is slightly more verbose, but it's also not some new syntax you have to figure out either.