# \[Idea\] Automatic conversion of tuples as function arguments

**URL:** <https://internals.rust-lang.org/t/idea-automatic-conversion-of-tuples-as-function-arguments/5687>\
**Category:** language design\
**Created:** [August 1, 2017, 12:39pm UTC](https://internals.rust-lang.org/t/idea-automatic-conversion-of-tuples-as-function-arguments/5687 "2017-08-01T12:39:11Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![marmistrz](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@marmistrz](https://internals.rust-lang.org/u/marmistrz)\
**Post date:** [August 1, 2017, 12:39pm UTC](https://internals.rust-lang.org/t/idea-automatic-conversion-of-tuples-as-function-arguments/5687/1 "2017-08-01T12:39:12Z")

</div>

Let’s have function

```rust
fn foo(x: i32, y: i32) -> i32 { x + y }

```

From the mathematical point of view, a function which takes two arguments is basically the same as a function which takes a two-tuple, because we can easily transform them either way without losing information.

```rust
fn foo2((x, y): (i32, i32)) -> i32 { x + y }

```

So what about allowing automatic destructuring here:

```rust
foo2(7, 15);
let x = (7, 15);
foo(x);

```

Is there any reason why Rust doesn’t support it other than _nobody has implemented it_?

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [August 1, 2017, 2:38pm UTC](https://internals.rust-lang.org/t/idea-automatic-conversion-of-tuples-as-function-arguments/5687/2 "2017-08-01T14:38:01Z")

</div>

For simple functions this might seem obvious, but once you get into generic functions or functions taking a tuple argument + more arguments this is less clear imo.

How many ways should I be able to call the following function?

```rust
fn foo2((x, y): (i32, i32), z: i32) -> i32 { x + y + z }

```

Maybe once we get variadic generics we could implement something like `(5, 6).map(foo)`, where all tuples implement `map` for `FnOnce(arg_num_is_tuple_len) -> T`.

---

<div class="post-metadata">

**Author:** ![leonardo](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/leonardo/32/1344_2.png) [@leonardo](https://internals.rust-lang.org/u/leonardo)\
**Post date:** [August 1, 2017, 4:44pm UTC](https://internals.rust-lang.org/t/idea-automatic-conversion-of-tuples-as-function-arguments/5687/3 "2017-08-01T16:44:57Z")

</div>

> [@ker](#):
>
> Maybe once we get variadic generics we could implement something like (5, 6).map(foo),

Perhaps the name "apply" is more correct than "map". But while I agree I'd like a way to map/apply a tuple like that, I am not sure doing it automatically is a clean design. Unclean (or implicit) designs cause problems later.

In code like this:

```
fn main() {
    fn mul2(x: u32, y: u32) -> u32 { x * y }
    let a1 = [1, 2, 3];
    let a2 = [10, 20, 30];
    let p: u32 = a1.iter().zip(&a2).map(|(&x, &y)| mul2(x, y)).sum();
    println!("{}", p);
}

```

You wish to write:

`let p: u32 = a1.iter().zip(&a2).map(|t| t.apply(mul2)).sum();`

Is this worth having? I am not sure...

---

<div class="post-metadata">

**Author:** ![Gankra](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gankra/32/6002_2.png) [@Gankra](https://internals.rust-lang.org/u/Gankra)\
**Post date:** [August 1, 2017, 4:53pm UTC](https://internals.rust-lang.org/t/idea-automatic-conversion-of-tuples-as-function-arguments/5687/4 "2017-08-01T16:53:40Z")

</div>

The swift devs have spent the last couple years trying to backpeddle on implementing “tuple splatting” because it turns out to be a bit of a nightmare in the details.

> <https://github.com/apple/swift-evolution/blob/master/proposals/0029-remove-implicit-tuple-splat.md>

I’d rather not repeat their mistakes here.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [August 2, 2017, 5:33am UTC](https://internals.rust-lang.org/t/idea-automatic-conversion-of-tuples-as-function-arguments/5687/5 "2017-08-02T05:33:19Z")

</div>

I think the direction here, to avoid the magic, is something like variadic generics, so the example becomes

```rust
let x = (7, 15);
foo(...x);

```

Where it’s still easy to call a function with the values of the tuple, but it’s unambiguous.

---

<div class="post-metadata">

**Author:** ![marmistrz](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@marmistrz](https://internals.rust-lang.org/u/marmistrz)\
**Post date:** [August 3, 2017, 8:37am UTC](https://internals.rust-lang.org/t/idea-automatic-conversion-of-tuples-as-function-arguments/5687/6 "2017-08-03T08:37:50Z")

</div>

This is a good point and I somewhat agree with what @scottmcm suggested. Maybe there could be a macro, which converts a tuple to a list of arguments? Currently it’s impossible since macros have to return a single object.

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [August 3, 2017, 11:20am UTC](https://internals.rust-lang.org/t/idea-automatic-conversion-of-tuples-as-function-arguments/5687/7 "2017-08-03T11:20:05Z")

</div>

A macro alone wouldn’t help, since it cannot inspect the type of an arbitrary tuple `x` to know its size. Depending on what you’re trying to do with it, you can probably get the brunt of the work done by writing a trait that basically resembles `Fn{,Once,Mut}`.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [August 3, 2017, 10:54pm UTC](https://internals.rust-lang.org/t/idea-automatic-conversion-of-tuples-as-function-arguments/5687/8 "2017-08-03T22:54:13Z")

</div>

An extension trait can do this ([playground](https://play.rust-lang.org/?gist=8c9bec2719f11b4023213768daab4be0&version=stable))

```rust
fn main() {
    let x = (1, 2);
    let y = std::cmp::min.call_with_tuple(x);
    println!("{}", y);

    let z = i32::default.call_with_tuple(());
    println!("{}", z);
}

trait Ext<T> {
    type Output;
    fn call_with_tuple(self, x: T) -> Self::Output;
}

impl<T, TR> Ext<()> for T
where
    T: FnOnce() -> TR,
{
    type Output = TR;
    fn call_with_tuple(self, _: ()) -> Self::Output {
        self()
    }
}

impl<T, T0, T1, TR> Ext<(T0, T1)> for T
where
    T: FnOnce(T0, T1) -> TR,
{
    type Output = TR;
    fn call_with_tuple(self, x: (T0, T1)) -> Self::Output {
        self(x.0, x.1)
    }
}

```

---

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [August 4, 2017, 7:43pm UTC](https://internals.rust-lang.org/t/idea-automatic-conversion-of-tuples-as-function-arguments/5687/9 "2017-08-04T19:43:23Z")

</div>

This feature exists and is really useful in Python, but Python is a dynamic interpreted language and it has a much richer function-call syntax. I am not sure it would be useful in Rust. Also, we don’t tend to do things just for the mathematical elegance of it in Rust. You must have something you want to use it for or you wouldn’t have brought it up, so would you mind posting some concrete examples where you think it would make the _overall_ code clearer?

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:28am UTC](https://internals.rust-lang.org/t/idea-automatic-conversion-of-tuples-as-function-arguments/5687/10 "2019-03-25T08:28:52Z")

</div>

Closed on March 25, 2019, 8:28am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
