Git Product home page Git Product logo

Comments (5)

shoumikhin avatar shoumikhin commented on April 28, 2024 1

Looks cool :)
Do you propose to have it alongside the existing catch operator as an overload?
I'm afraid it will interfere with the existing Error-only catch, and the complier would silently pick one overload over another, or complain unless you clearly state the error type.
Anyhow, pull requests are more than welcome!

from promises.

shoumikhin avatar shoumikhin commented on April 28, 2024 1

@gloooooooomy let's close this issue for now, but don't hesitate to follow up if you have any ideas on how to deal with the current compiler limitations.

from promises.

gloooooooomy avatar gloooooooomy commented on April 28, 2024 1

@shoumikhin sorry for reply too late. :(
now i know exactly what you concerned. and I'll check if there's another way to avoid.

from promises.

gloooooooomy avatar gloooooooomy commented on April 28, 2024

Yes, i proposed catch operator as an overloaded function.
because compiler could infers the generic type only when state the error type.

.catch { (error: MyError) in
  // compiler picks generic catch operator.
}
.catch { (error: Error) in
  // compiler picks default catch. because generic type does not supports protocol.
}
.catch { error in
  // same as "error: Error"
}

also i made a pull requests, please review :)

from promises.

shoumikhin avatar shoumikhin commented on April 28, 2024

@gloooooooomy thank you, I love your proposal!

Just have a concern with the case like the following:

enum CustomError: Error { case test }

Promise<Void> {
  throw Test.Error.code42
}.then { _ in
  XCTFail()
}.catch { error in
  XCTAssertTrue(error == CustomError.test)  // Never executed.
}

I imagine, it's expected that the catch above to be executed w/o specifying the error type explicitly, since it must catch all Errors. Unfortunately, it's not, because the compiler translates that one-liner catch as your generic version and derives the type based on the condition in the test assert. I.e. it thinks the catch generic type must be CustomError. But Test.Error is thrown, in fact. So that catch is never executed, since types don't match. To fix that behavior, one should either specify the expected error type explicitly as (error: Error) in(which is not always convenient and can be omitted by mistake), or write more than one line of code inside the catch closure to force the compiler to pick a more suitable non-generic catch overload (and that is potentially error-prone, because the clients may not be aware about such subtle implications).

Ideally, we'd want something like default generic arguments be implemented in Swift to have only one version of catch, and actually make it generic, as you've proposed.

What do you think?

from promises.

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.