# More consistent function types

**URL:** <https://forums.swift.org/t/more-consistent-function-types/9765>\
**Category:** Pitches\
**Created:** [February 10, 2018, 5:54am UTC](https://forums.swift.org/t/more-consistent-function-types/9765 "2018-02-10T05:54:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rudkx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rudkx/32/20610_2.png) [@rudkx](https://forums.swift.org/u/rudkx)\
**Post date:** [February 10, 2018, 5:54am UTC](https://forums.swift.org/t/more-consistent-function-types/9765/1 "2018-02-10T05:54:35Z")

</div>

I wrote this pitch up this morning, but am just now finding the time to get it posted.

It's something that I've thought about on and off for a little while but was motivated to get written up today both because of a recent source compatibility issue that was raised (as mentioned in the pitch) and because it has potential ABI impact and thus needs to be addressed in the short term.

This was a quick write-up of "pitch" quality, and there is no implementation yet, but I wanted to get something out in short order to see if people think this is worth pursuing.

Looking forward to your feedback.

[https://github.com/rudkx/swift-evolution/blob/more-consistent-function-representation/proposals/9999-more-consistent-function-representation.md](https://github.com/rudkx/swift-evolution/blob/more-consistent-function-representation/proposals/9999-more-consistent-function-representation.md)

---

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [February 10, 2018, 10:18am UTC](https://forums.swift.org/t/more-consistent-function-types/9765/2 "2018-02-10T10:18:43Z")

</div>

But a function taking zero arguments isn’t the same as a function taking one, even if said argument is `Void`.

At first glance, it seems that any code that needs those to be equivalent is conceptually broken. I don’t understand why you think they should be interchangeable.

---

<div class="post-metadata">

**Author:** ![MutatingFunk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mutatingfunk/32/984_2.png) [@MutatingFunk](https://forums.swift.org/u/MutatingFunk)\
**Post date:** [February 10, 2018, 10:36am UTC](https://forums.swift.org/t/more-consistent-function-types/9765/3 "2018-02-10T10:36:36Z")

</div>

There's currently no way in Swift to achieve @rudkx's example. I'm not sure how the example is ‘conceptually broken’.

The proposal is conceptually similar to having `(_: (String, Int)) -> ()` be equivalent to `(String, Int) -> ()`. Bringing back tuple splat, but with well defined rules, has always been an option. This proposal is a start to achieving that, by defining a specific case.

I don't see generics-based variadics coming along any time soon, so this kind of feature is going to have supporters.

---

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [February 10, 2018, 11:25am UTC](https://forums.swift.org/t/more-consistent-function-types/9765/4 "2018-02-10T11:25:46Z")

</div>

> There’s currently no way in Swift to achieve @rudkx’s example. I’m not sure how the example is ‘conceptually broken’.

`() -> U` and `(T) -> U` shouldn't be considered mutual IS-A for the same reason `(Float) -> String` and `(Int, Double) -> String` aren't considered mutual IS-A; their parameter profiles don't match. The code in the Introduction, "fails: We cannot infer T from _no type_" is correct.

> The proposal is conceptually similar to having `(_: (String, Int)) -> ()` be equivalent to `(String, Int) -> ()`. Bringing back tuple splat, but with well defined rules, has always been an option. This proposal is a start to achieving that, by defining a specific case.
> 
> I don’t see generics-based variadics coming along any time soon, so this kind of feature is going to have supporters.

I wasn't around in the early Swift days; do we want the strange chumminess between tuples and function arguments from those early days back?

How would variadic generic parameters be a substitute for regulated tuple-splatting?

---

<div class="post-metadata">

**Author:** ![zoul](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zoul/32/9559_2.png) [@zoul](https://forums.swift.org/u/zoul)\
**Post date:** [February 10, 2018, 1:13pm UTC](https://forums.swift.org/t/more-consistent-function-types/9765/5 "2018-02-10T13:13:19Z")

</div>

> [@CTMacUser](#):
>
> () -\> U and (T) -\> U shouldn’t be considered mutual IS-A

In plain English, when `T` equals `Void` they are both functions that take nothing and return `U`, doesn’t that make sense?

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [February 10, 2018, 4:09pm UTC](https://forums.swift.org/t/more-consistent-function-types/9765/6 "2018-02-10T16:09:18Z")

</div>

> [@MutatingFunk](#):
>
> The proposal is conceptually similar to having (\_: (String, Int)) -\> () be equivalent to (String, Int) -\> (). Bringing back tuple splat, but with well defined rules, has always been an option. This proposal is a start to achieving that, by defining a specific case.

Indeed, when working with generics it would be Nice™ if “`(T) -> U`” could work for any number of arguments T and any number of return values U. Notably, when the only requirement is that the output of one function `f` matches the input of another function `g`, it would be great to write `g(f(x))` and have it Just Work™.

For the case of “simple” functions which do not use inout, variadic parameters, default arguments, nor any other tricky things, we could consider once again treating these simple function as maps from tuples to tuples, for the purpose of generics at least.

---

<div class="post-metadata">

**Author:** ![MutatingFunk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mutatingfunk/32/984_2.png) [@MutatingFunk](https://forums.swift.org/u/MutatingFunk)\
**Post date:** [February 10, 2018, 5:28pm UTC](https://forums.swift.org/t/more-consistent-function-types/9765/7 "2018-02-10T17:28:04Z")

</div>

> [@CTMacUser](#):
>
> I wasn’t around in the early Swift days; do we want the strange chumminess between tuples and function arguments from those early days back?
> 
> How would variadic generic parameters be a substitute for regulated tuple-splatting?

Seconding @Nevin's answer, which addresses these points.

A simplified use case is as follows, for an event object which notifies its subscribers with a value:

```
class Event<T> {
    func notify(_ value: T) { … }
    func subscribe(_ handler: (T) -> ()) { … }
}

let event = Event<(A, B)>()
event.subscribe {a, b in
    …
}
event.notify(a, b)

```

It's what a lot of people want from variadic generic parameters, with a very simple syntax which requires no special handling by the API author.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [February 10, 2018, 6:19pm UTC](https://forums.swift.org/t/more-consistent-function-types/9765/8 "2018-02-10T18:19:02Z")

</div>

> [@Nevin](#):
>
> Indeed, when working with generics it would be Nice™ if “(T) -\> U” could work for any number of arguments T and any number of return values U. Notably, when the only requirement is that the output of one function f matches the input of another function g, it would be great to write g(f(x)) and have it Just Work™.
> 
> For the case of “simple” functions which do not use inout, variadic parameters, default arguments, nor any other tricky things, we could consider once again treating these simple function as maps from tuples to tuples, for the purpose of generics at least.

This issue of implicit tuple splatting has been discussed extensively, and I'm not sure what we're going to get here by rehashing it. The feature was removed, with the determination that it'll never come back, in favor of some future explicit tuple splatting syntax.

I have trouble with this particular proposal here because, although practical, it's asking for one special exception to the decision above. It seems we're formalizing specific instances of implicit tuple splatting piecemeal when, as already settled, the solution is intended to be an explicit tuple splatting syntax on which we've made zero progress.

---

<div class="post-metadata">

**Author:** ![rudkx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rudkx/32/20610_2.png) [@rudkx](https://forums.swift.org/u/rudkx)\
**Post date:** [February 10, 2018, 8:29pm UTC](https://forums.swift.org/t/more-consistent-function-types/9765/9 "2018-02-10T20:29:27Z")

</div>

```swift
event.subscribe { a, b in }

```

I'll point out that this works today, but only because of another short-term hack that was added last year. Long term I think we would like to make this specific case (of a closure literal) work with the understanding that closure literals are already special in many ways, and once we have some implementation issues straightened out in the compiler we should be able to make this work in a more principled and deliberate fashion.

---

<div class="post-metadata">

**Author:** ![rudkx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rudkx/32/20610_2.png) [@rudkx](https://forums.swift.org/u/rudkx)\
**Post date:** [February 10, 2018, 8:53pm UTC](https://forums.swift.org/t/more-consistent-function-types/9765/10 "2018-02-10T20:53:13Z")

</div>

> [@xwu](#):
>
> This issue of implicit tuple splatting has been discussed extensively, and I’m not sure what we’re going to get here by rehashing it.

Agreed. It's a separate issue and shouldn't be conflated with this pitch.

> [@xwu](#):
>
> I have trouble with this particular proposal here because, although practical, it’s asking for one special exception to the decision above. It seems we’re formalizing specific instances of implicit tuple splatting piecemeal when, as already settled, the solution is intended to be an explicit tuple splatting syntax on which we’ve made zero progress.

I would suggest another way of interpreting this proposal.

It is eliminating the odd asymmetry between function arguments and function results. All functions must have a result type, and if it's not explicitly declared, it defaults to `()`. Likewise, if a `return` appears in such a function, but the `()` is elided, the compiler provides it. If no `return` appears, both the `return` and the returned value `()` are provided.

```swift
  func impliedReturnType() { return } 

```

is desugared to:

```swift
  func impliedReturnType() -> () { return () }

```

The symmetric approach for arguments would be taking:

```swift
  func impliedArgument() -> Int { return 42 }

```

and desugaring it to:

```swift
  func impliedArgument(_: () = ()) -> Int { return 42 }

```

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [February 10, 2018, 9:08pm UTC](https://forums.swift.org/t/more-consistent-function-types/9765/11 "2018-02-10T21:08:33Z")

</div>

> [@rudkx](#):
>
> It is eliminating the odd asymmetry between function arguments and function results. All functions must have a result type, and if it’s not explicitly declared, it defaults to (). Likewise, if a return appears in such a function, but the () is elided, the compiler provides it. If no return appears, both the return and the returned value () are provided.

That's...interesting. It makes sense, and I can see why that consistency can come in handy. In this model, then, there's no such thing as a function that takes zero arguments.

The argument for symmetry is imperfect, though, because `return` specifically only allows one value to be returned, not zero, two, three, or more. By contrast, without implicit tuple splatting, a function explicitly takes any nonzero number of arguments. Eliminating zero but not two, three, four, etc. wouldn't necessarily improve consistency. And making two, three, four, etc., arguments equivalent to one tuple argument _is_ the issue of tuple splatting.

---

<div class="post-metadata">

**Author:** ![rudkx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rudkx/32/20610_2.png) [@rudkx](https://forums.swift.org/u/rudkx)\
**Post date:** [February 10, 2018, 9:40pm UTC](https://forums.swift.org/t/more-consistent-function-types/9765/12 "2018-02-10T21:40:36Z")

</div>

> [@xwu](#):
>
> In this model, then, there’s no such thing as a function that takes zero arguments.

Correct.

> [@xwu](#):
>
> The argument for symmetry is imperfect, though, because return specifically only allows one value to be returned, not zero, two, three, or more.

Yes, I will grant you the point that there isn't perfect symmetry, but that's by intentional design of the language. Rather than each function taking a tuple and returning a tuple, each function takes some number of arguments and returns a single result. So I don't see the lack of perfect symmetry as a flaw so much as a concession that we aren't aiming for perfect symmetry. This change just makes things slightly more symmetrical and consistent in this one way.

The current implementation _does_ model multiple argument functions as tuples, and that's a poor choice if only because tuples do not in the general case allow for things like `inout` elements.

We're slowly moving toward the right implementation, but each step in that direction tends to break existing code in subtle and surprising ways (the issue in the JIRA that motivated sending this pitch at this time was broken in exactly this way - a small tweak to the representation).

---

<div class="post-metadata">

**Author:** ![hooman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hooman/32/43536_2.png) [@hooman](https://forums.swift.org/u/hooman)\
**Post date:** [February 11, 2018, 3:34am UTC](https://forums.swift.org/t/more-consistent-function-types/9765/13 "2018-02-11T03:34:33Z")

</div>

Mark is right, it _is_ a symmetry issue.

> [@xwu](#):
>
> The argument for symmetry is imperfect, though, because return specifically only allows one value to be returned, not zero, two, three, or more. By contrast, ...

No, you should look at it the other way around. Instead of focusing on what it allows, look at what it prohibits: It prohibits zero return value, although it has sugar to make it appear as if it allows it.

In general, functions _have_ argument(s) and return _a_ result. He wants to enforce having (at least one) argument the same way Swift already enforces having a return value. This is very useful.

Consider the following case I ran into recently:

I had to write these four different overloads to cover the generic code I was writing:

```
() -> () -> T
(T) -> () -> U
() -> (T) -> U
(T) -> (U) -> V

```

While with Mark's (@rudkx) proposal, the following single overload would have been enough:

(T)-\>(U)-\>V

The symmetry issue is also apparent here:

`(T) -> U` matches `(T) -> Void` (a function with no return value)  
but it does not match `() -> U`, (a function with no argument).

This clearly breaks symmetry and creates a lot of extra boilerplate for a certain kind of generic code.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [February 11, 2018, 3:43am UTC](https://forums.swift.org/t/more-consistent-function-types/9765/14 "2018-02-11T03:43:15Z")

</div>

> [@hooman](#):
>
> In general, functions _have_ argument(s) and return a result.

I already get Mark's point, and he gets mine. It's not necessary to restate it here. But FWIW, that sentence is the fudge factor that I'm talking about:

Functions may have any number of arguments, including zero arguments or two arguments, but must have exactly one result. To restore symmetry, we need tuple splatting.

---

<div class="post-metadata">

**Author:** ![hooman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hooman/32/43536_2.png) [@hooman](https://forums.swift.org/u/hooman)\
**Post date:** [February 11, 2018, 4:20am UTC](https://forums.swift.org/t/more-consistent-function-types/9765/15 "2018-02-11T04:20:33Z")

</div>

> [@xwu](#):
>
> To restore symmetry, we need tuple splatting.

Can you clarify how tuple splatting would solve the particular four generic overloads example that I gave?

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [February 11, 2018, 4:29am UTC](https://forums.swift.org/t/more-consistent-function-types/9765/16 "2018-02-11T04:29:58Z")

</div>

You would use tuple splatting to pass any number of arguments to your function of type `(T) -> (U) -> V`. And then you would use tuple splatting to pass any number of arguments to your function of type `(U) -> V` that you get back.

Or am I misunderstanding your use case?

---

<div class="post-metadata">

**Author:** ![hooman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hooman/32/43536_2.png) [@hooman](https://forums.swift.org/u/hooman)\
**Post date:** [February 11, 2018, 4:34am UTC](https://forums.swift.org/t/more-consistent-function-types/9765/17 "2018-02-11T04:34:11Z")

</div>

> [@xwu](#):
>
> Functions may have any number of arguments, including zero arguments or two arguments, but must have exactly one result.

Also, where does this statement come from? [Mathematically speaking](https://en.wikipedia.org/wiki/Function_(mathematics)), a function has both input and output, although the domain of input and/or output might be an empty set. The symmetry of handling empty input vs empty output is broken now. It has nothing to do with tuple splatting.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [February 11, 2018, 4:47am UTC](https://forums.swift.org/t/more-consistent-function-types/9765/18 "2018-02-11T04:47:29Z")

</div>

> [@hooman](#):
>
> Also, where does this statement come from? Mathematically speaking, a function has both input and output, although the domain of input and/or output might be an empty set. The symmetry of handling empty input vs empty output is broken now. It has nothing to do with tuple splatting.

In Swift, function parameters and tuples were once treated identically, and therefore there was symmetry. Now, that's no longer the case because implicit tuple splatting has been removed. When a function has two parameters or zero parameters, you used to be able to pass it a tuple instead, and vice versa, but now you can't. That's what broke the symmetry. To restore that feature, we would need tuple splatting, except it must now be explicit instead of implicit.

This proposal doesn't restore full symmetry; it only makes a function that takes zero arguments equivalent to one that takes one argument, but does not address the general case. Anyway, I'm repeating myself now and this isn't productive.

---

<div class="post-metadata">

**Author:** ![MutatingFunk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mutatingfunk/32/984_2.png) [@MutatingFunk](https://forums.swift.org/u/MutatingFunk)\
**Post date:** [February 11, 2018, 10:00am UTC](https://forums.swift.org/t/more-consistent-function-types/9765/19 "2018-02-11T10:00:34Z")

</div>

If I'm understanding Xiaodi correctly, symmetry requires `(T) -> U` is convertible to and from all ‘map’ functions (`T` matching the input domain, `U` the output domain).

This was possible implicitly with tuple splatting, and could be restored with an explicit form of tuple splatting.

Tuple splat is undoubtedly related to this proposal, as its feature set is a strict superset what's proposed:

- `'(T) -> U where T == ()'` ↔ `'() -> U'` (proposal's use case)
- `'(T) -> U where T == (A)'` ↔ `'(A) -> U'` (single parameter)
- `'(T) -> U where T == (A, B)'` ↔ `'(A, B) -> U'` (multiple parameters)
- And so on for higher arity tuples…

However, from what I gather, the alternative suggestion is to aim for a different kind of symmetry, which could be defined as so:

- `'(T) -> U where T == ()'` ↔ `'(()) -> U'` ↔ `'() -> U'` (zero element sugar)
- `'(T) -> U where T == (A)'` ↔ `'((A)) -> U'` ↔ `'(A) -> U'` (single element)
- `'(T) -> U where T == (A, B)'` ↔ `'((A, B)) -> U'` (multiple elements)
- And so on for higher arity tuples…

Essentially, treating functions in generics as always taking a single tuple parameter (taking ‘parameter lists’ out of the picture), then providing usability sugar for the zero-tuple case.

---

<div class="post-metadata">

**Author:** ![hartbit](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hartbit/32/738_2.png) [@hartbit](https://forums.swift.org/u/hartbit)\
**Post date:** [February 11, 2018, 2:37pm UTC](https://forums.swift.org/t/more-consistent-function-types/9765/20 "2018-02-11T14:37:30Z")

</div>

> [@zoul](#):
>
> In plain English, when T equals Void they are both functions that take nothing and return U, doesn’t that make sense?

Void is not nothing. It’s the type of the empty tuple. The fact that it’s called Void is a misnomer.

[Next page](https://forums.swift.org/t/more-consistent-function-types/9765.md?page=2)
