47 Degrees joins forces with Xebia read more

Bow 0.4.0 is now available

Bow 0.4.0 is now available

Since the release of Bow for Swift earlier this year, we’ve made considerable effort towards making the library more accessible and easier to use. At the time of the release of version 0.3.0, we launched a microsite for the library aimed to serve as the starting point for developers interested in giving Bow a try. Many people have reached out to request documentation and additional resources on how to get started and that’s what we’ve provided in version 0.4.0.

API Reference

In part, our efforts have been devoted to adding documentation to the API reference. Bow has multiple modules, the most prominent of them being the core module; it contains useful data types and type classes that cover the primary scenarios that developers face in their daily tasks.

Version 0.4.0 provides documentation for the entire core module, which represents 55% of the entire library. You can get information about every type, type class, and function in Xcode as you would do with any other functionality in other Swift frameworks.

You can also visit the API Reference section in the site to search for specific types or methods. We use a customized version of Jazzy to generate these docs and they include all the modules of the library.

Documentation

The API Reference is a good resource for defining what the library does, but it’s not enough information to learn how to use the library. In order to overcome this problem, we have created a section for documentation covering topics ranging from how to get the library and the purpose of each module, to introductory articles on the main concepts and patterns for Functional Programming in Swift using Bow. New content will appear in this section as we make progress writing it.

In order to keep the documentation in sync with the latest version of Bow and make sure that all code examples are correct, we are writing these docs using Swift Playgrounds and compiling them with nef. The combination of these two tools lets us use the power of the IDE to write content with Markdown and Swift code with syntax highlighting and evaluation of expressions.

Then, in our Continuous Integration stage in Travis, we use nef to compile the docs and generate the pages that are automatically deployed to the site.

Easier to use

Besides the lack of documentation, there were other usability issues in the previous version of Bow, especially when we had to deal with writing polymorphic code. Fortunately, version 0.4.0 includes a full rework of the simulation for Higher Kinded Types and changes how type classes are encoded. The result is an important improvement in the ease of usage of the library.

Consider the following function for performing a division and reporting an error given a denominator of zero. With version 0.3.0 of the library, trying to write this type of function in a polymorphic way that abstracts the return type away and uses the type class ApplicativeError becomes cumbersome to write and use:

enum DivisionError {
    case byZero
}

func divide<F, ApplFE>(_ x: Int, _ y: Int, _ applicativeError: ApplFE) -> Kind<F, Int> where
    ApplFE: ApplicativeError, ApplFE.F == F, ApplFE.E == DivisionError {
  guard y != 0 else { return applicativeError.raiseError(.byZero) }
  return applicativeError.pure(x / y)
}

let result: Either<DivisionError, Int> = divide(4, 2, Either<DivisionError, Int>.applicativeError())

The signature of the divide function is complex to understand and calling the function is also hard, as users need to know how to get an instance of ApplicativeError for the type they want to get as a result.

With the new encoding, things look much cleaner:

func divide<F: ApplicativeError>(_ x: Int, _ y: Int) -> Kind<F, Int> where
  F.E == DivisionError {
  guard y != 0 else { return F.raiseError(.byZero) }
  return F.pure(x / y)
}

let result = Either.fix(divide(4, 2))
// or alternatively
let result2: Either<DivisionError, Int> = divide(4, 2)^

With the new encoding, the signature of the function gets much easier to write and the compiler resolves the instance of the type class that we need to use. The fix method or the ^ operator help the compiler cast the result to the right type.

Other changes

Finally, this release includes support for Swift 5 and some additional changes like the inclusion of the Selective type class based on a recent paper called Selective Applicative Functors. Selective is a type class in the Functor hierarchy that extends Applicative with some additional functions to dynamically determine which effects need to be applied.

Besides, we have worked adding convenience methods to some of the data types in the core module. Compared to their counterparts in the Arrow library for Kotlin, we’re still missing quite a few, so we would appreciate your contributions.

With version 0.4.0, Bow has taken a big leap in terms of quality. There’s still plenty of work to do, but we would like to hear from you during this process. Are you using Bow? Are you facing any problems? Let us know and help us move forward in our mission to bring Functional Programming to Swift with Bow!

Please check out the following Bow resources. Comments and questions are welcome and encouraged!

Bow is proudly sponsored by 47 Degrees, a Functional Programming consultancy with a focus on the Scala, Kotlin, and Swift Programming languages which supports the active development of Bow.

Ensure the success of your project

47 Degrees can work with you to help manage the risks of technology evolution, develop a team of top-tier engaged developers, improve productivity, lower maintenance cost, increase hardware utilization, and improve product quality; all while using the best technologies.