Cats - Extension Methods For Future/Task Conversion
See original GitHub issueRather 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:
- Created 7 years ago
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
andTask.fromFuture
which allows us to move the futures to the edge.Probably important to wait for @rossabaker to chime in.
Closed By #916