Questions tagged [multiple-return-values]
54 questions
121
votes
6 answers
Multiple values in single-value context
Due to error handling in Go, I often end up with multiple values functions. So far, the way I have managed this has been very messy and I am looking for best practices to write cleaner code.
Let's say I have the following function:
type Item struct…
![](../../users/profiles/2641547.webp)
Spearfisher
- 7,303
- 17
- 59
- 115
82
votes
9 answers
Is it pythonic for a function to return multiple values?
In python, you can have a function return multiple values. Here's a contrived example:
def divide(x, y):
quotient = x/y
remainder = x % y
return quotient, remainder
(q, r) = divide(22, 7)
This seems very useful, but it looks like it…
![](../../users/profiles/4883.webp)
readonly
- 306,152
- 101
- 198
- 201
22
votes
2 answers
Return map like 'ok' in Golang on normal functions
In Go, the following works (note one use of the map has one return, the other has two returns)
package main
import "fmt"
var someMap = map[string]string { "some key": "hello" }
func main() {
if value, ok := someMap["some key"]; ok {
…
![](../../users/profiles/704936.webp)
Michael Wasser
- 1,600
- 1
- 17
- 31
8
votes
1 answer
Can constexpr-if-else bodies return different types in constexpr auto function?
I'm trying to write a function that maps an enumeration of values to a set of types based on the runtime value of the enumeration. I realize that you cannot return different types based on the runtime value of an enumeration because the compiler…
![](../../users/profiles/562174.webp)
Short
- 7,669
- 1
- 23
- 29
7
votes
1 answer
Initialization of multiple members using multiple return value
Since C++17 I can do
std::pair init () {
return std::make_pair (1, 1.2);
}
void foo () {
const auto [x, y] = init ();
std::cout << x << " " << y << "\n";
}
That is cool, but is there any way I can initialize multiple…
![](../../users/profiles/3671410.webp)
Artur Pyszczuk
- 1,860
- 1
- 13
- 21
7
votes
3 answers
Multiple return value and := in go
Why is this a valid program?
package main
import "fmt"
func giveMeError(limit int) ([]string, error) {
return nil, fmt.Errorf("MY ERROR %d", limit)
}
func main() {
res1, err := giveMeError(1)
if err == nil {
…
![](../../users/profiles/692499.webp)
Tony
- 30,345
- 9
- 45
- 77
6
votes
3 answers
How to pass multiple return values to a variadic function?
I have a Go function which returns two integer values. Below is the function
func temp() (int, int){
return 1,1
}
Is it possible to put temp function directly into a Println and print both the outputs using string formatting as…
![](../../users/profiles/8898218.webp)
InAFlash
- 4,199
- 3
- 29
- 45
6
votes
5 answers
Switch with enum and multiple return
I have two objects:
public enum BugReportStatus
{
OpenUnassigned = 0,
OpenAssigned = 1,
ClosedAsResolved = 2,
ClosedAsRejected = 3
}
and
public enum BugReportFilter
{
Open = 1,
...
Closed = 4,
}
And I would like to…
![](../../users/profiles/3289652.webp)
Pickeroll
- 757
- 2
- 7
- 18
4
votes
1 answer
How do I compare the results of two functions with multiple return values in one expression?
I have written a test utility function aequals (assert equals) that expects an actual result and an expected result as arguments. I use it like this:
aequals(fib(8), 21);
But now I have a function with multiple return values:
function stuff()…
![](../../users/profiles/472245.webp)
towi
- 20,210
- 25
- 94
- 167
4
votes
2 answers
Golang: Can you type a returned interface{} in one statement?
Let's say I have this:
type Donut string
type Muffin string
func getPastry () (interface{}, error) {
// some logic - this is contrived
var d Donut
d = "Bavarian"
return d, nil
}
Is it possible to reduce this to one line:
p, err :=…
![](../../users/profiles/607453.webp)
raindog308
- 759
- 1
- 8
- 18
4
votes
2 answers
Assigning elements from returned array where index is 1 or higher
Given is a function, that returns an array with (n) elements.
I want to assign these return values, but not the first one.
// match returns ["abc123","abc","123"] here
[ foo, bar ] = "abc123".match(/([a-z]*)([0-9]*)/);
Now I've got
foo =…
![](../../users/profiles/3911769.webp)
jawo
- 818
- 8
- 19
3
votes
1 answer
Java switch use case
I'm reluctant to use a switch, but I saw switch will be improved in Java 12
Java 12 added the switch expression as an experimental feature. A Java switch expression is a switch statement which can return a value.
The only use case I found (before…
![](../../users/profiles/7294900.webp)
user7294900
- 47,183
- 17
- 74
- 157
3
votes
5 answers
Can a function return multiple values of varying types?
It thought it would be interesting to return multiple values (with different types!) from a C++ function call.
So I've looked around to maybe found some example code but unfortunately I could not find anything matching to this topic.
I'd like a…
user9590073
2
votes
1 answer
How does Go determine the context in which multiple values can be used?
package main
import "fmt"
func multipleRets() (int, int, int, int) {
return 11, 22, 33, 44
}
func main() {
// Q1
fmt.Println(multipleRets()) // This is fine.
fmt.Println(1, multipleRets()) // But this one errors.
// Q2
…
![](../../users/profiles/9552116.webp)
Coconut
- 628
- 7
- 19
2
votes
0 answers
Result reshuffling during instruction selection
In an LLVM backend, during instruction selection, my input looks something like this:
t17: i16,ch = load t16:1, t2, undef:i16
I'd like to select an opcode that has some extra result as well, i.e. replace the above with something like
t17: i16, _:…
![](../../users/profiles/477476.webp)
Cactus
- 25,576
- 9
- 60
- 130