Skip to content

Instantly share code, notes, and snippets.

@sforteln
Last active August 29, 2015 14:21
Show Gist options
  • Save sforteln/e9fd7170a5126098af41 to your computer and use it in GitHub Desktop.
Save sforteln/e9fd7170a5126098af41 to your computer and use it in GitHub Desktop.
Swift 1.2 : Confusing error message for closures that do not return value
import UIKit
import Foundation
//set up
extension Array {
func filterMap<U>(transform: (T) -> U?) -> [U] {
var mapped = [U]()
for item in self {
if let unwrapped = transform(item) {
mapped.append(unwrapped)
}
}
return mapped
}
}
var array : [String] = []
/*
no error message as expected
*/
let transformedReturnsValue : [Int] = array.filterMap({
(data: String) -> Int? in
return 1
})
/*
If the closure has no statements in it you
get the helpful error message :
"Missing return in a closure expected to return 'Int?'"
*/
let transformedEmptyClosure : [Int] = array.filterMap({
(data: String) -> Int? in
//return 1
})
/*
If the closure has statements in it but no
return you get the confusing error message :
Cannot invoke 'filterMap' with an argument list of type '((String) -> Int?)'
*/
let transformedNoReturn : [Int] = array.filterMap({
(data: String) -> Int? in
println("foo")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment