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.

Cats - Extension Methods For Future/Task Conversion

See original GitHub issue

Rather than consistently wrapping entire function calls we could embed implicit classes within the org.http4s.util.Task implementation.

Something like

object task extends TaskFunctions {
  implicit class TaskToFutureConverter[A](task: Task[A]){
    def unsafeToFuture : Future[A] = unsafeTaskToFuture[A](task)
  }
  implicit class FutureToTaskConverter[A](future: => Future[A])(implicit ec: ExecutionContext) {
    def toTask : Task[A] = futureToTask(future)(ec)
  }
}

This would allow us to interact on the edge of blaze/http4s much easier. The only major question is if this is required, why isn’t it directly in fs2? However this would in the implementation of the writeEntityBody.

  def writeEntityBody(p: EntityBody): Task[Boolean] = {
    val writeBody : Task[Unit] = (p to writeSink).run
    val writeBodyEnd : Task[Boolean] = writeEnd(Chunk.empty).toTask
    writeBody >> writeBodyEnd
  }
  
  private val writeSink: Sink[Task, Byte] = { s =>
    val writeStream : Stream[Task, Unit] = s.chunks.evalMap[Task, Task, Unit](writeBodyChunk(_ , false).toTask)
    val errorStream : Throwable => Stream[Task, Unit] = e => Stream.eval(exceptionFlush().toTask).flatMap{_ => fail(e)}
    writeStream.onError(errorStream)
  }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ChristopherDavenportcommented, Feb 9, 2017

I would say that we can scratch that one, as it replaces the current implementation of unsafeTaskToFuture exactly, and it is on Task.

Infact on further investigation, we may be able scrap this whole package in favor of unsafeRunAsyncFuture and Task.fromFuture which allows us to move the futures to the edge.

Probably important to wait for @rossabaker to chime in.

0reactions
ChristopherDavenportcommented, Feb 12, 2017

Closed By #916

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implicits, type classes, and extension methods, part 3
The most infamous implicit conversion I know is scala. ... Such methods are called extension methods, classes that provide them usually have ...
Read more >
Nifty implicit conversions to Scala's Future to IO #173 - GitHub
I've been lifting the in Scala's Future to Async for a while now and came up with a simple implicit conversion for this:...
Read more >
Extension Methods - Scala 3 - EPFL
To convert a reference to an extension method, the compiler has to know about the extension method. We say in this case that...
Read more >
Ammonite
Converted all string-encoding methods to take a scala.io.Codec instead of a String or Charset , letting you pass ...
Read more >
9 tips about using cats in Scala you might want to know
9) Extension method constructors. Let's start with probably the most basic feature — extension methods for any type that convert an instance ...
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