Thanks Howard, it's a good workaround. Do you think it would be better to be part of swift language feature? I wish swift team could consider this :)
On Thu, Jul 6, 2017 at 11:04 AM Howard Lovatt <howard.lov...@gmail.com> wrote: > You could use a result type, e.g.: > > https://github.com/antitypical/Result > > Or roll your own result type. > > I think the reason that Swift doesn't support what you want is because of > rethrows. When you declare a method as rethrows it can throw anything > because the closure it is re-throwing can throw anything. They could have > fully typed the rethrows but obviously decided that was not worth it. At > present rethrows is light weight; the compiler generates two versions of > the method, one that throws and one that doesn't. If it was typed then it > would be like a generic method and a version of the method would be > required for each type combination that was actually used. > > -- Howard. > > On 6 July 2017 at 10:38, Tim Wang via swift-users <swift-users@swift.org> > wrote: > >> Hi Swifters, >> >> I am wondering if it is possible to specify error types thrown in a >> protocol method. By allowing us to do it and letting the compiler check all >> the implementations of the protocol to make sure only they would only throw >> the specified error types, we only need to catch our error types when >> calling these methods. >> >> For the code below: >> >> enum MyError: Error { >> >> case justError >> >> } >> >> protocol MethodWillThrow { >> >> func testMethod() throws MyError >> >> } >> >> >> extension MethodThrow { >> >> func testMethod() throws { >> >> throw MyError.justError >> >> } >> >> } >> >> class TestClass: MethodThrow { >> >> func testMethod() throws { >> >> throw MyError.justError >> >> } >> >> func anotherMethod() { >> >> do { >> >> try testMethod() >> >> } catch MyError.justError { >> >> print("my error") >> >> } *catch {* >> >> * print("other error")* >> >> * }* >> >> } >> >> } >> >> Now we need add this extra default catch to make it compile and work and >> I really want to remove this catch. >> >> Please let me know if there is a way to do it and thanks for help in >> advance. >> >> Tim Wang >> >> _______________________________________________ >> swift-users mailing list >> swift-users@swift.org >> https://lists.swift.org/mailman/listinfo/swift-users >> >>
_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users