8

I was looking for a data type for asynchronous operations.

I found that scalaz.ContT[Trampoline, Unit, ?] supports all features in scalaz.concurrent.Future, in addition of BindRec.

Though, there are more utilities for scalaz.concurrent.Future than scalaz.ContT[Trampoline, Unit, ?], e.g. an Applicative instance runs Futures in parallel.

I thought those utilities can be implemented for ContT[Trampoline, Unit, ?] as well.

Why did the author create a new Future-based scalaz-concurrent library, instead of reusing ContT?

Yang Bo
  • 3,586
  • 3
  • 22
  • 35

1 Answers1

2

At the risk of being facetious, one key benefit is that Future is a named concept that lots of people will recognise and understand, for example it has its own wikipedia page.

I don't recognise the ContT type which you say is equivalent. I tried to find it in the scalaz docs, but it is listed there with no explanation. Where can I read more about this, and how do you know it is equivalent to a Future?

You say that "an Applicative instance runs Futures in parallel"; would multiple ContT operations not be run in parallel? That is a key feature of Futures, and might be an answer your question.

UPDATE:

I see now that ContT is an implementation of continuation passing style, and that scalaz.ContT[Trampoline, Unit, ?] is a special case of a continuation passing function that may be isomorphic to Future, if certain external assumptions hold.

I think that the answer to your question is the same reasons that many other special cases are given prominence when they could perhaps be thought of as one case of a more general structure:

  • It is easier to discuss and easier to name
  • It is more efficient, as the implementation can take advantage of some special cases that may not hold in the general structure
  • It is easier to reason about, for example here we can be sure that Futures are executed in parallel, whereas we don't know that about continuation passing functions in general. (Of course, it is possible to execute Futures in sequence, as is done in some test frameworks, but that would not be considered a conventional implementation of Future)
  • It is a useful concept and it benefits programmers to highlight it and make it easily accessible
Rich
  • 15,048
  • 2
  • 66
  • 119
  • You can use Scalaz features via type classes. You can see there are more built-in type classes in ContT's companion than Futures. – Yang Bo Apr 09 '17 at 12:06
  • From a Scala user's point of view, "parallel" may be an answer. However, it's not a proper answer from the author's point of view. Because `ContT`'s data structure is isomorphic to `Future`. If the author want to give `ContT` the same ability, he can simply copy and paste the implementation of `choiceAny` form `Future`. – Yang Bo Apr 09 '17 at 12:12