Swift: Subscripting assignment returns Void

Originator:rix.rob
Number:rdar://17412887 Date Originated:22-Jun-2014 07:28 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode6-Beta2 (6A216f)
Classification:Serious Bug Reproducible:Always
 
Summary:
Subscripting assignment returns Void, rendering it wordy and frustrating to try to compose with.


Steps to Reproduce:
1. Given a dictionary, `memo`,
2. Write a method like so:

func compute<T : Hashable>(parameter: T) -> Int {
    return memo[parameter] = expensiveComputation(parameter)
}


Expected Results:
I expected that the result of this assignment expression would be the value inserted into the dictionary, and thus that the function’s return value would be the integer result of the expensiveComputation(…) call


Actual Results:
Instead, the compiler errors out saying that it can’t convert (), i.e. Void, to the return type.


Regression:
N/A


Notes:
Assignment expressions having values allows for less boilerplate. It’s not just brevity; it’s that it boosts the signal vs. the noise. Instead, you have to do this:

let v = expensiveComputation(parameter)
memo[parameter] = v
return v

Now you have to think about the value of the constant v, as well as about control flow, whereas expressed as it was, it is a simple declaration that the return value is the memoized value is the result of this computation.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!