question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tagless Final instead of hard-coded futures

See original GitHub issue

I’ve been digging through the code a little and noticed that the direct use and execution of Futures brings some inconveniences with it:

  • An ExecutionContext has to be passed everywhere.
  • You can’t use other libraries like Monix to perform the execution.
  • You can’t reflect on the execution (e.g. in what sequence is stuff done).
  • Tests always have to await.

I was wondering if you have considered / would consider a Final Tagless encoding instead of direct use of futures?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:17
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
DougCcommented, Nov 1, 2018

For Field it might look like this:

case class Field[Ctx, Val, F[_] : Effect](...)

In fact, in most places you would not need to declare F[_] as an effect. So it might look like this:

case class Field[F[_], Ctx, Val](...)

cats-effect provides a number of type classes that describe the needed effect more granularly, e.g. Async, Sync, Concurrent. But even then, a lot of code only needs to declare F[_] to be something like Functor (if you use map) or Monad (if you use flatMap), etc.

Your point about how this looks to the user of the library is valid and something to consider. One approach could be to have the core library be parameterized on F[_], which would be compatible with http4s and other parts of the cats-effect ecosystem, and then provide a version of the types that are specialised to Future for backwards compatibility and easier use when integrating into, say, Play or Akka Http.

4reactions
nightscapecommented, Dec 5, 2018

I just stumbled upon another interesting use case, optimizing the execution: https://typelevel.org/blog/2017/12/27/optimizing-final-tagless.html The author @LukaJCB even mentions GraphQL as possible use case 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can you give me a brief understanding on tagless final? : r/scala
Tagless Final, in essence, is about using traits instead of specific classes as your dependencies. Just as you might define traits for your...
Read more >
Tagless final — from a different perspective | by Daniel Balazs
Without tagless final. In this case, the application uses cats IO as an effect type and that is hard-coded everywhere. Peter checks the...
Read more >
Tagless final vs. akka-http routes - miklos.blog
Have you ever wondered how could you set up some akka-http routes for your tagless final program? Did you end up hardcoding your...
Read more >
Introduction to Tagless final - Beyond the lines
Obviously the Id monad isn't stack-safe but Future or Task are. It's also possible to use a Trampoline instead of the Id monad...
Read more >
The Tagless Final Pattern - Toby Hobson
The Tagless Final pattern allows us to abstract over the effect, for example allowing us wrap code in a Future or Id (no...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found