Hi, I'm coming from Ruby where we can do
def method_that_returns_two_values
return 1, "abc"
end
v1, v2 = method_that_returns_two_values
v1 #=> 1
v2 #=> "abc"
Do we have a comparable approach in Nim, or should I just return an Array or Seq with two values?
Specifically, your example would look like this in Nim using tuples:
proc methodThatReturnsTwoValues(): (int, string) =
return (1, "abc")
let (v1, v2) = methodThatReturnsTwoValues()
echo "v1=", v1, " v2=", v2
Looks nearly the same:
proc procThatReturnsTwoValues: (int, string) =
return (1, "abc")
let (v1, v2) = procThatReturnsTwoValues()
echo v1 #=> 1
echo v2 #=> abc
But you can also use the result variable instead of using return. Or you can even just write proc procThatReturnsTwoValues: auto = (1, "abc")
And you can give the tuple entries names with proc procThatReturnsTwoValues: tuple[a: int, b: string] =