Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve errors around Output.eval and Output#flatMap / ToFuture @implicitNotFound #443

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/src/main/scala/besom/internal/Output.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ class Output[+A] private[internal] (using private[besom] val ctx: Context)(
yield nested.flatten
)

/** Mock variant of flatMap that will fail at compile time if used with a function that returns a value instead of an Output.
*
* @param f
* function to apply to the value of the Output
*/
inline def flatMap[B](f: A => B): Nothing = scala.compiletime.error(
"""Output#flatMap can only be used with functions that return an Output or a structure like scala.concurrent.Future, cats.effect.IO or zio.Task.
If you want to map over the value of an Output, use the map method instead."""
)

def zip[B](that: => Output[B])(using z: Zippable[A, B]): Output[z.Out] =
Output.ofData(dataResult.zip(that.getData).map((a, b) => a.zip(b)))

Expand Down
8 changes: 8 additions & 0 deletions core/src/main/scala/besom/internal/Result.scala
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ object Result:
_ <- finalizersRef.update(_.updatedWith(scope)(finalizers => Some(release(a) :: finalizers.toList.flatten)))
yield a

@implicitNotFound(
"""Could not find a given ToFuture instance for type ${F}.

Besom offers the following instances:
* besom-core provides a ToFuture instance for scala.concurrent.Future
* besom-zio provides a ToFuture instance for zio.Task
* besom-cats provides a ToFuture instance for cats.effect.IO"""
)
trait ToFuture[F[_]]:
def eval[A](fa: => F[A]): () => Future[A]
end Result
Expand Down
50 changes: 50 additions & 0 deletions core/src/test/scala/besom/internal/OutputTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,56 @@ class OutputTest extends munit.FunSuite:
Context().waitForAllTasks.unsafeRunSync()
}

test("Output#flatMap on non supported datatypes have nice and informative compile error") {
val shouldCompile = scala.compiletime.testing.typeCheckErrors(
"""import besom.*
import besom.internal.{Output, DummyContext}
import besom.internal.RunOutput.{*, given}
given besom.internal.Context = DummyContext().unsafeRunSync()

val out: Output[Int] = Output(1).flatMap(x => Output(x + 1))"""
)

assert(shouldCompile.isEmpty)

val shouldNotCompile = scala.compiletime.testing.typeCheckErrors(
"""import besom.*
import besom.internal.{Output, DummyContext}
import besom.internal.RunOutput.{*, given}
given besom.internal.Context = DummyContext().unsafeRunSync()

val out: Output[Int] = Output(1).flatMap(x => Option(x + 1))"""
)

assert(shouldNotCompile.size == 1)

val expected = """Could not find a given ToFuture instance for type Option.
|
|Besom offers the following instances:
| * besom-core provides a ToFuture instance for scala.concurrent.Future
| * besom-zio provides a ToFuture instance for zio.Task
| * besom-cats provides a ToFuture instance for cats.effect.IO""".stripMargin

assertEquals(shouldNotCompile.head.message, expected)

val shouldNotCompileFlatMapRawValue = scala.compiletime.testing.typeCheckErrors(
"""import besom.*
import besom.internal.{Output, DummyContext}
import besom.internal.RunOutput.{*, given}
given besom.internal.Context = DummyContext().unsafeRunSync()

val out: Output[Int] = Output(1).flatMap(_ => 1)"""
)

assert(shouldNotCompileFlatMapRawValue.size == 1)

val expectedFlatMapRawValue =
"""Output#flatMap can only be used with functions that return an Output or a structure like scala.concurrent.Future, cats.effect.IO or zio.Task.
|If you want to map over the value of an Output, use the map method instead.""".stripMargin

assertEquals(shouldNotCompileFlatMapRawValue.head.message, expectedFlatMapRawValue)
}

Vector(
(true, "value", Some("value")),
(false, "value", None)
Expand Down
Loading