Comments (5)
Curious. What error does swiftc
give you?
from result.
it shows me that simpleBlock should be marked with "throws"
from result.
Looks like a bug of Swift compiler, because simpleBlock
below is inferred as () throws -> Void
in spite of that try tryIsSuccess("success")
is inside the auto closure and Result(_:)
never throws errors.
func simpleFunc() {
let result: Result<String, NSError> = Result(try tryIsSuccess("success"))
}
// simpleBlock is inferred as `() throws -> Void`.
let simpleBlock = {
let result: Result<String, NSError> = Result(try tryIsSuccess("success"))
}
I found the radar that points out the same bug.
https://openradar.appspot.com/22437049
from result.
Yes. It's a Swift compiler bug.
But I think the non-autoclusure version init is also necessary.
for example, this is my code to use the non-autoclusure version init of Result:
(This is an AVFoundation AVAsset loading result handler)
asset.loadValuesAsynchronouslyForKeys(keys) {[weak asset, weak self] in
let result = Result{ [weak asset] in
guard let asset = asset else { throw Break.Unknown }
for key in keys {
var error: NSError?
switch asset.statusOfValueForKey(key, error: &error) {
case .Failed : throw Break.Error(error!)
case .Cancelled: return nil // return nil means no result, because the cancel result has been handled elsewhere
case .Loaded : continue
default : throw Break.Unknown
}
}
return asset.playable && !asset.hasProtectedContent
}
if let result = result {
self?.outputHandler(result)
}
}
and this is my extension for Result:
extension Result{
public init?(_ f: () throws -> T?) {
do {
guard let result = try f() else { return nil }
self = .Success(result)
} catch {
self = .Failure(error as! Error)
}
}
}
from result.
Iām š to adding a non-@autoclosure
variant as you suggest. Would you be willing to submit a PR?
from result.
Related Issues (20)
- Support for higher order functions that can throw HOT 1
- Add Result to swift compatibility check HOT 1
- Replace `NoError` with `Never`? HOT 1
- Lower the `Error: Swift.Error` constraint on `Result` temporarily. HOT 4
- Why not declare 'Result' as 'Result<T>'? So it easy to deal with different types of errors. HOT 6
- [Disabled code coverage for xCode 9] HOT 1
- Unable to use the Result namespace HOT 3
- Using `init(attempt:)` always produces `Result` with `AnyError` type HOT 6
- Non-throwing dematerialize for result using NoError HOT 1
- Change (some) flatMap to compactMap? HOT 2
- 4.0 release HOT 1
- Adding a `fold` method HOT 5
- Result vs. Throws README section is out of date HOT 1
- Result initializer works in for loop but not map? HOT 1
- support required for swift 4.2 HOT 2
- How to pass an array of Swift.Errors as the right (Error) type? HOT 3
- Example os import usage for Swift 5/4 HOT 9
- Migration to Swift 4 HOT 1
- Cocoapods instructions still pin to v4 HOT 3
- Swift 5 support? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from result.