{"id":23814002,"url":"https://github.com/dmytromitin/dotty-patched","last_synced_at":"2025-09-06T23:31:49.352Z","repository":{"id":62296594,"uuid":"558981340","full_name":"DmytroMitin/dotty-patched","owner":"DmytroMitin","description":"Eval library and patched Scala-3/Dotty compiler. Evaluating source code and trees at compile time hacking multi-staging programming","archived":false,"fork":false,"pushed_at":"2022-11-02T02:26:40.000Z","size":1933,"stargazers_count":19,"open_issues_count":15,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-01-29T04:41:42.066Z","etag":null,"topics":["compilation","compiler","dotty","eval","evaluation","macros","metaprogramming","runtime-compilation","scala","scala-3","scala3","scala3-metaprogramming"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DmytroMitin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-10-28T18:35:37.000Z","updated_at":"2023-10-25T11:49:40.000Z","dependencies_parsed_at":"2022-10-30T06:45:35.224Z","dependency_job_id":null,"html_url":"https://github.com/DmytroMitin/dotty-patched","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DmytroMitin%2Fdotty-patched","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DmytroMitin%2Fdotty-patched/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DmytroMitin%2Fdotty-patched/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DmytroMitin%2Fdotty-patched/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DmytroMitin","download_url":"https://codeload.github.com/DmytroMitin/dotty-patched/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232149970,"owners_count":18479563,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compilation","compiler","dotty","eval","evaluation","macros","metaprogramming","runtime-compilation","scala","scala-3","scala3","scala3-metaprogramming"],"created_at":"2025-01-02T03:46:08.821Z","updated_at":"2025-01-02T03:46:09.426Z","avatar_url":"https://github.com/DmytroMitin.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dotty-patched and Eval\n\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.dmytromitin/eval_3/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.dmytromitin/eval_3)\n[![Maven Central](https://img.shields.io/maven-central/v/com.github.dmytromitin/eval_3.svg?label=maven%20central\u0026color=success)](https://search.maven.org/search?q=g:%22com.github.dmytromitin%22%20AND%20a:%22eval_3%22)\n[![Sonatype Snapshots](https://img.shields.io/nexus/r/https/oss.sonatype.org/com.github.dmytromitin/eval_3.svg?color=success)](https://oss.sonatype.org/content/groups/public/com/github/dmytromitin/eval_3/)\n[![javadoc](https://javadoc.io/badge2/com.github.dmytromitin/eval_3/javadoc.svg)](https://javadoc.io/doc/com.github.dmytromitin/eval_3)\n[![Scaladex](https://index.scala-lang.org/dmytromitin/dotty-patched/latest.svg?color=success)](https://index.scala-lang.org/dmytromitin/dotty-patched)\n[![Scaladex](https://index.scala-lang.org/dmytromitin/dotty-patched/eval/latest.svg?color=success)](https://index.scala-lang.org/dmytromitin/dotty-patched/eval)\n[![Scaladex](https://index.scala-lang.org/dmytromitin/dotty-patched/scala3-compiler-patched/latest.svg?color=success)](https://index.scala-lang.org/dmytromitin/dotty-patched/scala3-compiler-patched)\n[![Scaladex](https://index.scala-lang.org/dmytromitin/dotty-patched/scala3-compiler-patched-assembly/latest.svg?color=success)](https://index.scala-lang.org/dmytromitin/dotty-patched/scala3-compiler-patched-assembly)\n[![Join the chat at https://gitter.im/DmytroMitin/eval](https://badges.gitter.im/DmytroMitin/eval.svg)](https://gitter.im/DmytroMitin/eval?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\n[mvnrepository](https://mvnrepository.com/artifact/com.github.dmytromitin)\n[repo1.maven](https://repo1.maven.org/maven2/com/github/dmytromitin/)\n\n## Background\n\nhttps://github.com/lampepfl/dotty\n\nhttps://stackoverflow.com/questions/71390113/get-annotations-from-class-in-scala-3-macros\n\nhttps://stackoverflow.com/questions/70945320/how-to-compile-and-execute-scala-code-at-run-time-in-scala3\n\nhttps://github.com/DmytroMitin/dotty-patched/commit/fdf010ca4901b22961f3ae1cb3459b5fa194652e\n\n## Dotty-patched and multi-staging in Scala 3 macros\n`staging.run` [evaluates](https://docs.scala-lang.org/scala3/reference/metaprogramming/staging.html) a typed tree (wrapped into an `Expr`) into a value \n(this seems similar to `context.eval`/`toolbox.eval` evaluating an untyped tree in [Scala 2](https://docs.scala-lang.org/overviews/reflection/symbols-trees-types.html#tree-creation-via-parse-on-toolboxes)).\nThis functionality exists in Scala 3/Dotty but is deliberately blocked in [macros](https://docs.scala-lang.org/scala3/reference/metaprogramming/macros.html) \n(because of the [phase consistency principle](https://docs.scala-lang.org/scala3/reference/metaprogramming/macros.html#the-phase-consistency-principle)). \nTo unblock, a code expanding macros should be [compiled](https://www.scala-sbt.org/1.x/docs/Configuring-Scala.html#Configuring+Scala+tool+dependencies) with the compiler patched. \nMacros themselves can be compiled with the standard compiler. \nStaging dependency didn't have to be patched so far.\n```scala\nscalaVersion := \"3.2.1\" // 3.2.0, 3.1.3, ...\nlibraryDependencies += scalaOrganization.value %% \"scala3-staging\" % scalaVersion.value\n// custom Scala settings\nmanagedScalaInstance := false\nivyConfigurations += Configurations.ScalaTool\nlibraryDependencies ++= Seq(\n  scalaOrganization.value  %  \"scala-library\"  % \"2.13.10\",\n  scalaOrganization.value  %% \"scala3-library\" % \"3.2.1\",\n  \"com.github.dmytromitin\" %% \"scala3-compiler-patched-assembly\" % \"3.2.1\" % \"scala-tool\"\n)\n```\n```scala\nimport scala.quoted.*\n\ninline def printAtCompileTime[A](a: A): Unit = ${impl[A]('a)}\n\ndef impl[A: Type](a: Expr[A])(using Quotes): Expr[Unit] =\n  import quotes.reflect.*\n  given staging.Compiler = staging.Compiler.make(this.getClass.getClassLoader)\n  val aValue = staging.run(a) // evaluating a tree\n  println(aValue)\n  '{()}\n```\n```scala\nsbt clean compile\n```\n```scala\nprintAtCompileTime(1 + 1) // 2 (at compile time)\n```\n## Eval\nOn contrary to `staging.run`, `Eval` evaluates into a value a source code rather than a tree. So it works on standard Scala 3.\n```scala\nscalaVersion := \"3.2.1\" // or 3.2.0\nlibraryDependencies += \"com.github.dmytromitin\" %% \"eval\" % \"0.1\"\n```\n```scala\nsbt clean compile run\n```\n```scala\nimport com.github.dmytromitin.eval.Eval\n\nEval[Int](\"1 + 1\") // 2 (at runtime)\n```\n## Eval in Scala 3 macros\n```scala\nimport com.github.dmytromitin.eval.Eval\nimport scala.quoted.*\n\ninline def printAtCompileTime[A](a: A): Unit = ${impl[A]('a)}\n\ndef impl[A: Type](a: Expr[A])(using Quotes): Expr[Unit] = \n  import quotes.reflect.*\n  val str = a.asTerm.pos.sourceCode.getOrElse(\n    report.errorAndAbort(s\"No source code for ${a.show}\")\n  )\n  val aValue = Eval[A](str) // evaluating source code\n  println(aValue)\n\n  '{()}\n```\n```scala\nsbt clean compile\n```\n```scala\nprintAtCompileTime(1 + 1) // 2 (at compile time)\n```\n## Some built-in Scala 3 macros\n```scala\nimport com.github.dmytromitin.macros.eval.AnnotationsMacro.getAnnotations\nimport com.github.dmytromitin.macros.eval.PrintValueMacro.printAtCompileTime\n\nprintAtCompileTime(1 + 1)\n//2\ngetAnnotations(classOf[scala.CanEqual[?, ?]])\n//new scala.annotation.internal.SourceFile(\"library/src/scala/CanEqual.scala\"), new scala.annotation.internal.Child[scala.CanEqual.derived.type](), new scala.annotation.implicitNotFound(\"Values of types ${L} and ${R} cannot be compared with == or !=\")\n```\n```scala\n// add custom Scala settings to build.sbt, see above\nimport com.github.dmytromitin.macros.run.AnnotationsMacro.getAnnotations\nimport com.github.dmytromitin.macros.run.PrintValueMacro.printAtCompileTime\n\nprintAtCompileTime(1 + 1)\n//2\ngetAnnotations(classOf[scala.CanEqual[?, ?]])\n//scala.List.apply[java.lang.String](\"new scala.annotation.internal.SourceFile(\\\"library/src/scala/CanEqual.scala\\\")\", \"new scala.annotation.internal.Child[scala.CanEqual.derived.type]()\", \"new scala.annotation.implicitNotFound(\\\"Values of types ${L} and ${R} cannot be compared with == or !=\\\")\")\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmytromitin%2Fdotty-patched","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdmytromitin%2Fdotty-patched","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmytromitin%2Fdotty-patched/lists"}