# Disambiguate Return Type With Void

**URL:** <https://forums.swift.org/t/disambiguate-return-type-with-void/2355>\
**Category:** Discussion\
**Created:** [April 26, 2016, 1:57pm UTC](https://forums.swift.org/t/disambiguate-return-type-with-void/2355 "2016-04-26T13:57:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anton\_Zhilin](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@Anton\_Zhilin](https://forums.swift.org/u/Anton_Zhilin)\
**Post date:** [April 26, 2016, 1:57pm UTC](https://forums.swift.org/t/disambiguate-return-type-with-void/2355/1 "2016-04-26T13:57:30Z")

</div>

+1

> Also, technically we can assign a value to Void function

This proposal will not disallow that. Just non-Void functions will be  
preferred when result is used.

array.sort(..) // mutating  
let array2 = array.sort(..) // non-mutating, instead of array.sorted()

I think, that's actually the best solution to mutating / non-mutating  
convention!

Andrew, why not generalize this proposal to functions with @unusedResult?

In terms of "precedence" when result is used:  
Non-Void \> @unusedResult \> Void (+warning)

When unused:  
Void \> @unusedResult \> Non-Void (+warning)

- Anton

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [April 26, 2016, 4:26pm UTC](https://forums.swift.org/t/disambiguate-return-type-with-void/2355/2 "2016-04-26T16:26:57Z")

</div>

There are mutating methods that have return values, like Dictionary.removeValue(forKey:). Admittedly there’s no non-mutating version of that at the moment, but that doesn’t mean there couldn’t be.

Jordan

> **···**
>
> > On Apr 26, 2016, at 06:57, Антон Жилин via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > +1
> > 
> > \> Also, technically we can assign a value to Void function  
> > This proposal will not disallow that. Just non-Void functions will be preferred when result is used.  
> > array.sort(..) // mutating  
> > let array2 = array.sort(..) // non-mutating, instead of array.sorted()  
> > I think, that's actually the best solution to mutating / non-mutating convention!

---

<div class="post-metadata">

**Author:** ![Anton\_Zhilin](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@Anton\_Zhilin](https://forums.swift.org/u/Anton_Zhilin)\
**Post date:** [April 26, 2016, 5:41pm UTC](https://forums.swift.org/t/disambiguate-return-type-with-void/2355/3 "2016-04-26T17:41:16Z")

</div>

Let's expand on that situation. Dictionary would have these methods:

@discardableResult func removeValue(forKey key: Key) -\> Value?  
func removeValue(forKey key: Key) -\> Self

I would argue that these two versions are not semantically identical: (1)  
returns removed value while (2) does not.  
Therefore they should have different names. In this case, we can rename  
them like this:

@discardableResult func removeValue(forKey key: Key) -\> Value?  
func remove(key key: Key) -\> Self

Or like this:

func removeValue(forKey key: Key) -\> Value?  
func remove(key key: Key) -\> Void  
func remove(key key: Key) -\> Self

- Anton

> **···**
>
> 2016-04-26 19:26 GMT+03:00 Jordan Rose \<jordan\_rose@apple.com\>:
> 
> > On Apr 26, 2016, at 06:57, Антон Жилин via swift-evolution \< \> swift-evolution@swift.org\> wrote:
> > 
> > +1
> > 
> > \> Also, technically we can assign a value to Void function  
> > This proposal will not disallow that. Just non-Void functions will be  
> > preferred when result is used.
> > 
> > array.sort(..) // mutating  
> > let array2 = array.sort(..) // non-mutating, instead of array.sorted()
> > 
> > I think, that's actually the best solution to mutating / non-mutating convention!
> > 
> > There are mutating methods that have return values, like  
> > Dictionary.removeValue(forKey:). Admittedly there’s no non-mutating version  
> > of that at the moment, but that doesn’t mean there couldn’t be.
> > 
> > Jordan

---

<div class="post-metadata">

**Author:** ![Andrew](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew/32/949_2.png) [@Andrew](https://forums.swift.org/u/Andrew)\
**Post date:** [April 27, 2016, 9:01am UTC](https://forums.swift.org/t/disambiguate-return-type-with-void/2355/4 "2016-04-27T09:01:08Z")

</div>

I'm happy to add `@unusedResult`, with a little more discussion. I'm not  
sure when it would actually be used though.

I'm not sure if you can define both of these functions:

&nbsp;&nbsp;&nbsp;@unusedResult func example() -\> Int  
&nbsp;&nbsp;&nbsp;func example() -\> Int

Either way, a method with @unusedResult should probably have a different  
name to one without.

You can define both of these functions:

&nbsp;&nbsp;&nbsp;@unusedResult func example() -\> Int  
&nbsp;&nbsp;&nbsp;func example() -\> String

However, if the methods return different non-void values they should  
probably have different names.

> **···**
>
> On Tue, Apr 26, 2016 at 11:57 PM, Антон Жилин \<antonyzhilin@gmail.com\> wrote:
> 
> > +1
> > 
> > \> Also, technically we can assign a value to Void function  
> > This proposal will not disallow that. Just non-Void functions will be  
> > preferred when result is used.
> > 
> > array.sort(..) // mutating  
> > let array2 = array.sort(..) // non-mutating, instead of array.sorted()
> > 
> > I think, that's actually the best solution to mutating / non-mutating convention!
> > 
> > Andrew, why not generalize this proposal to functions with @unusedResult?
> > 
> > In terms of "precedence" when result is used:  
> > Non-Void \> @unusedResult \> Void (+warning)
> > 
> > When unused:  
> > Void \> @unusedResult \> Non-Void (+warning)
> > 
> > - Anton

---

<div class="post-metadata">

**Author:** ![Anton\_Zhilin](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@Anton\_Zhilin](https://forums.swift.org/u/Anton_Zhilin)\
**Post date:** [April 27, 2016, 11:44am UTC](https://forums.swift.org/t/disambiguate-return-type-with-void/2355/5 "2016-04-27T11:44:05Z")

</div>

Agreed. That would only allow to disambiguate call with unused result, but  
would not solve this problem. Jordan pointed at it.  
It would be consistent therefore to leave @discardableResult functions with  
usual non-Void functions.

- Anton

> **···**
>
> 2016-04-27 12:01 GMT+03:00 Andrew Bennett \<cacoyi@gmail.com\>:
> 
> > I'm happy to add `@unusedResult`, with a little more discussion. I'm not  
> > sure when it would actually be used though.
> > 
> > I'm not sure if you can define both of these functions:
> > 
> > &nbsp;&nbsp;&nbsp;@unusedResult func example() -\> Int  
> > &nbsp;&nbsp;&nbsp;func example() -\> Int
> > 
> > Either way, a method with @unusedResult should probably have a different  
> > name to one without.
> > 
> > You can define both of these functions:
> > 
> > &nbsp;&nbsp;&nbsp;@unusedResult func example() -\> Int  
> > &nbsp;&nbsp;&nbsp;func example() -\> String
> > 
> > However, if the methods return different non-void values they should  
> > probably have different names.
> > 
> > On Tue, Apr 26, 2016 at 11:57 PM, Антон Жилин \<antonyzhilin@gmail.com\> \> wrote:
> > 
> > > +1
> > > 
> > > \> Also, technically we can assign a value to Void function  
> > > This proposal will not disallow that. Just non-Void functions will be  
> > > preferred when result is used.
> > > 
> > > array.sort(..) // mutating  
> > > let array2 = array.sort(..) // non-mutating, instead of array.sorted()
> > > 
> > > I think, that's actually the best solution to mutating / non-mutating convention!
> > > 
> > > Andrew, why not generalize this proposal to functions with @unusedResult?
> > > 
> > > In terms of "precedence" when result is used:  
> > > Non-Void \> @unusedResult \> Void (+warning)
> > > 
> > > When unused:  
> > > Void \> @unusedResult \> Non-Void (+warning)
> > > 
> > > - Anton
