Closed
Description
Minimized code
import scala.quoted._
case class FileName private(name: String)
object FileName {
def unsafe(s: String) = new FileName(s)
implicit inline def ToFileName(s: String): FileName =
${createFileName('{s})}
def fileNameFromString(s: String): Either[String, FileName] =
Right(FileName.unsafe(s))
def createFileName(fileName: Expr[String])(using qctx: QuoteContext): Expr[FileName] =
fileName match {
case e@Const(s) =>
'{fileNameFromString($e) match {
case Right(fn) =>
fn
case Left(_) =>
${qctx.throwError(s"$s is not a valid file name! It must not contain a /", fileName)}
}}
case _ =>
qctx.throwError(s"$fileName is not a valid file name. It must be a literal string", fileName)
}
}
val fileName1: FileName = ToFileName("fileName1")
Output
-- Error: /Users/etorreborre/projects/specs2/specs2-five/common/shared/src/test/scala/org/specs2/io/DirectoryPathSpec.scala:13:38
[error] 13 | val fileName1: FileName = ToFileName("fileName1")
[error] | ^^^^^^^^^^^
[error] | fileName1 is not a valid file name! It must not contain a /
Expectation
It looks like the pattern match is always matching on the error case Left
even if the function returns Right
. My expectation is that pattern matching should work in quotations.
Please tell me if there's any alternative formulation which would work.