# In what sense are Scala's Try[T] and Future[T] dual?

In a recent course based on Scala I found a hint that the Scala types Try[T] and Future[T] are dual. This was explained only informally and the reader was referred to duality in category theory for a more precise definition.

I have some basic knowledge of category theory but I cannot see how these two types can correspond to categories that are dual to each other.

So, what categories correspond to the Scala types Try[T] and Future[T], and why are these categories dual?

• Duality doesn't necessarily mean "there is a category in which these concepts are categorically dual. It can be a bit more general than that.
– cody
Dec 6, 2013 at 18:23
• @cody: I gave this interpretation because the author mentioned duality in category theory (and provided a link to a wikipedia page en.wikipedia.org/wiki/Dual_%28category_theory%29). Of course, my interpretation can be wrong / not general enough. Dec 7, 2013 at 9:03
• @Giorgio In an informal sense! Scala and its typing system are quite complex and, as far as I'm aware, not currently known to correspond to any particular category. Scala's futures are to do with concurrency, and concurrency doesn't not fit well with categories. Dec 7, 2013 at 9:16
• I was confused by this part of the coursera class as well. Aug 11, 2015 at 20:57

The Try[T] type is basically the sum type $T + E$, where $E$ is the type of exceptions.
A typical way of rendering a future Future[T] is via temporal logic. Here, a future can be understood as a "weak eventually" operator, $\Diamond T$, saying that at some point in the future you may receive a $T$. This can be decomposed as the coinductive type $\nu \alpha.\;T + \bullet\alpha$, where $\bullet$ is the "next-step" modality.
• Thanks for the answer, I will definitely look at temporal logic! I would like to point out that Future[T] in Scala accepts a callback of type Try[T] -> Unit. The callback is registered using the method Future[T].onComplete(). I.e. when the future completes successfully or with an error, it calls all registered callback with the resulting Try[T] value. So can it be that the Scala interpretation of futures corresponds to $\diamond (T + E)$ instead of $\diamond T$? Dec 10, 2013 at 18:17