Giter VIP home page Giter VIP logo

Comments (8)

ahernandezlopez avatar ahernandezlopez commented on June 13, 2024 3

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.

neilpa avatar neilpa commented on June 13, 2024

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.

connectdotz avatar connectdotz commented on June 13, 2024

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.

neilpa avatar neilpa commented on June 13, 2024

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.

connectdotz avatar connectdotz commented on June 13, 2024

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.

neilpa avatar neilpa commented on June 13, 2024

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 Boxed values).

from result.

ikesyo avatar ikesyo commented on June 13, 2024

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.

gfontenot avatar gfontenot commented on June 13, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.