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

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