Comments (8)
Have you tried Result<Void, MyErrorType>
?
enum MyErrorType: ErrorType {
case None
}
I was wondering kind of the same some weeks ago @ http://stackoverflow.com/questions/30875889/implement-an-equatable-void-none-type/33142599#33142599 :-)
from result.
An empty array is perfectly reasonable for the result if your query returns a collection of values. If it's a scalar that's optional, define it as such, e.g. Result<Int?, SomeError>
from result.
tried that, but here is the weird result:
enum Whatever: ErrorType {
case e1, e2
}
func test1() -> Result<String?, Whatever> {
return .Success(nil)
}
if let s = test1().value {
print("s should not be nil: \(s)")
} else {
print("s is nil")
}
and we got:
s should not be nil: nil
from result.
That's because value
returns T?
which means String??
if you plug in your types. The if-let
unwraps the outer optional of the returned Some(nil)
, hence printing nil.
from result.
Sure, but the question remain that how do we elegantly retrieve the actual value without verbose workaround
if let s1 = test1().value, s = s1 ...
if let s = (test1().value ?? nil) ...
Is there a better way to do this double-optional unwrapping? Can the value property auto unwrap the optional before returning? Ideally if let s = test1.value
should work for either Result<String,Error>
or Result<String?,Error>
...
from result.
I wouldn't call it a "verbose workaround", that's just when dealing with multiple nested optionals. Alternatively, if you're using Swift 2 you could pattern match over the result and avoid value
entirely. (This doesn't work as well in Swift 1.2 due to the Box
ed values).
from result.
You can write the case as follows in Swift 2:
if case let s?? = test1().value {
print("s should not be nil: \(s)")
} else {
print("s is nil")
}
from result.
I'm going to go ahead and close this. If it's still an issue or if these solutions don't work for you, feel free to re-open.
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.