Swift 2.0 - throws tip

Quick Swift 2.0 throws tip:

There's been a lot of «heated» debates regarding the throws approach added to Swift 2.0 in favor of Either<T,U>.


Some examples:


That being said, I'm not going to convince you to use it or avoid it. That's entirely up to you, the app / framework you are working on and wether or not you need to do async calls with throws (which is a «fun» thing to accomplish).


If you end up using the throws approach though there's a big issue along the lines of «What is this function supposed to throw at me?»

And while this tip won't «solve» the issue at compile time is sure a nice way to «notify» the consumers of your API somehow.

My approach so far was to add the following to the header of the apple doc of my functions:

  /**
  Do something nice here.

  - parameter dragon: 🐉

  - returns: A rainbow 🌈

  - throws `CustomError`: Thrown when a unicorn is born.
  */
  public func winTheInternet(dragon: Dragon) throws -> Rainbow {

This will turn into something like this:

Take away

As you can see this is not a «perfect» solution but is at least a great way of providing documentation for your callers as part of your internal documentation.

Happy documentation! 😉

Posted by Esteban Torres on 6/14/2015


Comments

Comments are published on the website GitHub repository. You can leave a comment on this post's issue. Your comment will show up automatically on this page.