func loadData(file: String) throws IOError -> Data { ... } func main(files: [String]) throws IOError { // loadDataの IOError が .map から返ってきて欲しい try files.map { try loadData(file: $0) } }
(edited)extension Array { func map<U>(_ f: (Element) throws -> U) rethrows -> [U] }
↑このrethrowsは、throwsのところのエラー型を投げる、としたいけどextension Array { func map<U>(_ f: (Element) throws -> U) rethrows -> [U] { var ret: [U] = [] for i in indices { let x = self[i] do { ret.append(try f(x)) } catch (error) { throw ArrayMapError(index: i, element: x, underlyingError: error) } } return ret } }