PromiseKit icon indicating copy to clipboard operation
PromiseKit copied to clipboard

'catch' is now a reserved keyword in Swift 2.0, making it invalid for use as a method name

Open apendley opened this issue 11 years ago • 31 comments

Propose renaming to "failed" or something similar.

apendley avatar Jun 08 '15 21:06 apendley

Can I confirm you tested this?

mxcl avatar Jun 08 '15 21:06 mxcl

I have not yet tested this yet, however the new reference is available at https://itunes.apple.com/us/book/swift-programming-language/id1002622538?mt=11.

In this reference, "catch" has been added as a reserved keyword (look in the section 'Lexical Structure', subsection 'Keywords and Punctuation')

I will be downloading the Xcode 7 beta later today, at which time I will test this to verify, and will update here after I've done so.

apendley avatar Jun 08 '15 21:06 apendley

Yeah, I'm downloading too. This would suck if true, but such is life.

mxcl avatar Jun 08 '15 21:06 mxcl

so is defer

schemers avatar Jun 08 '15 21:06 schemers

Oh FFS.

mxcl avatar Jun 08 '15 22:06 mxcl

Options to replace catch:

  • p.then{ /*…*/ }.error { error in }
  • p.then{ /*…*/ }.onfail { error in }
  • p.then{ /*…*/ }.fail { error in }
  • p.then{ /*…*/ }.failed { error in }
  • p.then{ /*…*/ }.clasp { error in }
  • p.then{ /*…*/ }.onerror { error in }

Options for defer:

  • let (promise, fulfill, reject) = Promise<Int>.deferred()
  • let (promise, fulfill, reject) = Promise<Int>.tuple()
  • let (promise, fulfill, reject) = Promise<Int>.breakOut()
  • let (promise, fulfill, reject) = Promise<Int>.split()

mxcl avatar Jun 08 '15 22:06 mxcl

FWIW: For catch replacement, I like fail or failed.

For defer replacement I like deferred(). My rationale for this is that using the alternate form: let deferred = Promise<Int>.deferred() seems more natural than the other alternatives, as would accessing it's members: deferred.promise, deferred.fulfil, deferred.reject.

apendley avatar Jun 08 '15 22:06 apendley

fail is not my favorite, it doesn’t read as well as catch. I'm still thinking of more ideas, the above list is not the final list.

mxcl avatar Jun 09 '15 17:06 mxcl

Coming from JS promises, deferred was sometimes used and seems like a natural replacement. fail was also used by jQuery in place of catch, though their promises aren't compliant. Not that javascript needs to have any influence on whatever you decide.

ZerdaH avatar Jun 10 '15 17:06 ZerdaH

Preliminary swift 2.0 branch here: https://github.com/mxcl/PromiseKit/tree/swift-2.0

It compiles, some UI tests are disabled. The name snatch is not final.

mxcl avatar Jun 11 '15 06:06 mxcl

What about rescue? This is used in Ruby and I guess some other languages. IMO It's give a good insight of what it is doing, juste like catch

pjechris avatar Jun 12 '15 15:06 pjechris

The current branch is now rescue. Though I'm not 100% sure yet since PromiseKit doesn't rescue errors, since the app survives if you don't.

mxcl avatar Jun 13 '15 22:06 mxcl

I pushed some additional revisions so you can throw in most swift methods.

mxcl avatar Jun 13 '15 22:06 mxcl

Not really sure that rescue is a great name for the new catch which @mxcl has already mentioned doesn't actually rescue from errors. Additionally there is already a recover feature which does rescue from errors and recovery is a synonym for rescue

mowens avatar Jun 15 '15 17:06 mowens

Also, Swift “exceptions” don’t crash the app either; instead you must handle them, you cannot write code that does not.

Ideally we’d keep the same pattern for Promise, but I fail to see how that can be done since we are not writing the compiler.

mxcl avatar Jun 15 '15 19:06 mxcl

Has the decision been made yet? If not, what about p.then{ /*…*/ }.ifFailed { error in } p.then{ /*…*/ }.whenFailed { error in } ?

andriichernenko avatar Jul 17 '15 14:07 andriichernenko

No decision. I'm having sleepless nights about it.

mxcl avatar Jul 17 '15 15:07 mxcl

As for defer, I haven't used it much, so I might not completely understand its meaning, but handle (as in door handle) seems like a candidate to me: let (promise, fulfill, reject) = Promise<Int>.handle() or let (promise, fulfill, reject) = Promise<Int>.createHandle()

Or maybe some other, better name for object which can be used to do stuff.

andriichernenko avatar Jul 17 '15 15:07 andriichernenko

For defer I made it pendingPromise which is more Cocoa like anyway.

mxcl avatar Jul 17 '15 15:07 mxcl

I think I’m happy with report:

spinner.start()

when(fetchImage(), getLocation()).then { image, location in
    self.imageView.image = image;
    self.label.text = "Buy your cat a house in \(location)"
}.ensure {
    spinner.stop()
}.report { error in
    UIAlertView(…).show()
}

I changed finally to ensure since we had to change away from catch and finally is from the try, catch, finally pattern. And finally was a lie with the Swift version since 2.0 anyway since you couldn't do it after a catch.

I will probably add ensure and report as aliases for AnyPromise, but I don't wish to force the change when not necessary for all users.

Opinions welcome.

mxcl avatar Aug 02 '15 07:08 mxcl