Giter VIP home page Giter VIP logo

Comments (5)

robrix avatar robrix commented on June 3, 2024

Curious. What error does swiftc give you?

from result.

frogcjn avatar frogcjn commented on June 3, 2024

it shows me that simpleBlock should be marked with "throws"

from result.

ishkawa avatar ishkawa commented on June 3, 2024

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.

frogcjn avatar frogcjn commented on June 3, 2024

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.

robrix avatar robrix commented on June 3, 2024

Iā€™m šŸ‘ to adding a non-@autoclosure variant as you suggest. Would you be willing to submit a PR?

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.