Scala3: Crafting Types Through Metaprogramming?

Just in case, here is what I meant by hiding ViewOf inside a type class (type classes is an alternative to match types). Sadly, in Scala 3 this is wordy. (version 1) import scala.annotation.experimental import scala.quoted.{Expr, Quotes, Type, quotes} // Library part trait View extends Selectable { def applyDynamic(key: String)(args: Any*): Any = { println(s”$key … Read more

How to compile and execute scala code at run-time in Scala3?

Scala 2 version of this answer is here: How can I run generated code during script runtime? In Scala 3: For example you can use Li Haoyi‘s Ammonite ammonite.Main(verboseOutput = false).runCode(“””println(“Hello, World!”)”””) // Hello, World! build.sbt scalaVersion := “3.1.3” libraryDependencies += “com.lihaoyi” % “ammonite” % “2.5.4-22-4a9e6989” cross CrossVersion.full excludeDependencies ++= Seq( ExclusionRule(“com.lihaoyi”, “sourcecode_2.13”), ExclusionRule(“com.lihaoyi”, “fansi_2.13”), … Read more

Howto model named parameters in method invocations with Scala macros?

Here’s an implementation that’s also a little more generic: import scala.language.experimental.macros object WithIdExample { import scala.reflect.macros.Context def withId[T, I](entity: T, id: I): T = macro withIdImpl[T, I] def withIdImpl[T: c.WeakTypeTag, I: c.WeakTypeTag](c: Context)( entity: c.Expr[T], id: c.Expr[I] ): c.Expr[T] = { import c.universe._ val tree = reify(entity.splice).tree val copy = entity.actualType.member(newTermName(“copy”)) val params = copy … Read more

Scala macros: What is the difference between typed (aka typechecked) and untyped Trees

Theoretical part This is an architectural peculiarity of scalac that started leaking into the public API once we exposed internal compiler data structures in compile-time / runtime reflection in 2.10. Very roughly speaking, scalac’s frontend consists of a parser and a typer, both of which work with trees and produce trees as their result. However … Read more

How to access parameter list of case class in a dotty macro

Using standard type class derivation in Dotty import scala.deriving.Mirror case class ParseError(str: String, msg: String) trait Decoder[T]{ def decode(str:String): Either[ParseError, T] } object Decoder { given Decoder[String] with { override def decode(str: String): Either[ParseError, String] = Right(str) } given Decoder[Int] with { override def decode(str: String): Either[ParseError, Int] = str.toIntOption.toRight(ParseError(str, “value is not valid Int”)) … Read more

Method Override with Scala 3 Macros

With new method Symbol.newClass (Scala 3.1.3) this becomes quite easy: import scala.annotation.experimental import scala.quoted.* object NewClass { inline def newClass[A]: A = ${newClassImpl[A]} @experimental def newClassImpl[A: Type](using Quotes): Expr[A] = { import quotes.reflect.* val name: String = TypeRepr.of[A].typeSymbol.name + “Impl” val parents = List(TypeTree.of[A]) def decls(cls: Symbol): List[Symbol] = List(Symbol.newMethod(cls, “func”, MethodType(List(“s”))(_ => List(TypeRepr.of[String]), _ … Read more