Skip to content

Instantly share code, notes, and snippets.

@etorreborre
Created April 5, 2020 17:44
Show Gist options
  • Save etorreborre/8f0a06392b8dbc1a13b7ad44bb4fb764 to your computer and use it in GitHub Desktop.
Save etorreborre/8f0a06392b8dbc1a13b7ad44bb4fb764 to your computer and use it in GitHub Desktop.
Bug with Dotty macros?
case class FileName private(name: String)
/**
val fileName1: FileName = ToFileName("fileName1")
-- 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 /
*/
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment