Logo

Programming-Idioms

  • JS

Idiom #126 Multiple return values

Write a function foo that returns a string and a boolean value.

#include <tuple>
std::tuple<std::string, bool> foo() {
  return std::make_tuple("someString", true);
}

c++ 11
#include <tuple>
auto foo()
{
    return std::make_tuple("Hello", true);
}

use automatic return type deduction to save having to repeat the type
const foo = () => ({string: 'string', bool: true})

Usage:
let {string: a, bool: b} = foo ()
const foo = () => ['string', true];
#include <stdbool.h>
typedef struct {
    const char * const a;
    const bool b;
} RetStringBool;

RetStringBool foo() {
    return (RetStringBool) {.a = "Hello", .b = true};
}

New implementation...
< >
MLKo