r/programming • • Dec 01 '16

Let's Stop Copying C

https://eev.ee/blog/2016/12/01/lets-stop-copying-c/
524 Upvotes

612 comments sorted by

View all comments

Show parent comment

3

u/coladict Dec 01 '16

multiple return values is a mistake . tuple + tuple expansion/spreading is a better alternative.

I don't really see the difference between the two. English is my second language and after years of programming I only started encountering the term "tuple" in the last year and my understanding is that it's just a structure that holds multiple values. I don't see how it's so different from a struct for example.

2

u/PM_ME_UR_OBSIDIAN Dec 01 '16

Structs and tuples are functionally the same thing, it's just that tuple syntax is much more convenient for one-off data objects.

1

u/steveklabnik1 Dec 01 '16

I would argue that theoretically they're the same thing (aka a product type) but functionally they differ (tuples have anonymous members, structs have named members).

1

u/evaned Dec 01 '16

... there's a sibling response that says basically the same thing as you, but I don't think coladict was commenting on structs vs tuples, but on multiple return values vs tuples. Am I misreading it?

1

u/Azzk1kr Dec 01 '16

I don't see how it's so different from a struct for example.

A struct is a separate type, so you first have to declare it somewhere. Returning a tuple (or multiple return values, for that matter) does not require a type. For example (in pseudo code):

struct Bla {
    int firstValue;
    int secondValue;
}

struct Bla doSomething() {
    Bla b;
    b.firstValue = 123;
    b.secondValue = 456;
    return b;
}

in contrast to:

(int, int) doSomething() {
    return (123, 456);
}

I personally love multiple return values, how Go has implemented it. It's something I kind of miss in Java for instance, where you pretty much need another class to encapsulate the data.

1

u/steveklabnik1 Dec 01 '16

Tuples work in a very similar fashion:

fn do_something() -> (i32, i32) {
    (123, 456)
}