Skip to content

Instantly share code, notes, and snippets.

View dotty-end.txt
compiler/src/dotty/tools/dotc/config/CommandLineParser.scala: end loop
compiler/src/dotty/tools/dotc/config/CommandLineParser.scala: end tokenize
compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala: end addOneBound
compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala: end addBoundTransitively
compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala: end dropSuperTraits
compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala: end widenInferred
compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala: end addConstraint
compiler/src/dotty/tools/dotc/core/Contexts.scala: end ops
compiler/src/dotty/tools/dotc/core/Contexts.scala: end comparing
compiler/src/dotty/tools/dotc/core/Decorators.scala: end extension

Principled Meta Programming for Scala

This note outlines a principled way to meta-programming in Scala. It tries to combine the best ideas from LMS and Scala macros in a minimalistic design.

  • LMS: Types matter. Inputs, outputs and transformations should all be statically typed.

  • Macros: Quotations are ultimately more easy to deal with than implicit-based type-lifting

  • LMS: Some of the most interesting and powerful applications of meta-programming

View ThrowableToNonFatal.scala
import scalafix.v1._
import scala.meta._
class ThrowableToNonFatal extends SemanticRule("ThrowableToNonFatal") {
override def fix(implicit doc: SemanticDocument): Patch = {
doc.tree.collect {
case c @ Case(Pat.Typed(v, Type.Name("Throwable")), _, _) =>
View grpc.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
$ scala -version
Scala code runner version 2.13.2 -- Copyright 2002-2020, LAMP/EPFL and Lightbend, Inc.
$ scala -Xprint:jvm -e 'if (util.Random.nextInt == 1) 2 else "3"'
[[syntax trees at end of jvm]] // scalacmd6816512509873541389.scala
package <empty> {
object Main extends Object {
def main(args: Array[String]): Unit = {
new <$anon: Object>();
View ScalazEitherInfix.scala
package scalaz
import scalafix.v1.{Patch, SemanticDocument, SemanticRule}
import scala.meta._
import scalafix.v1._
class ScalazEitherInfix extends SemanticRule("ScalazEitherInfix") {
override def fix(implicit doc: SemanticDocument): Patch = {
doc.tree.collect {
echo 'addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.15")' > project/scalafix.sbt
sbt scalafixEnable "scalafix" "test:scalafix"
rm project/scalafix.sbt
View 2.12.11.log
[info] running foo.Main
class foo.A
View ExpandPolymorphicLambdas.scala
import scalafix.v1.{Patch, SyntacticDocument, SyntacticRule}
import scala.meta.{Name, Term, Transformer, Tree, Type, XtensionQuasiquoteType}
class ExpandPolymorphicLambdas extends SyntacticRule("ExpandPolymorphicLambdas") {
override def description: String = "Expand kind-projector's syntax for polymorphic lambda values"
override def isLinter: Boolean = false
private def replacePlaceholder(tree: Term, param: Term.Name): Option[Term] = tree match {
case Term.Select(Term.Placeholder(), method) => Some(Term.Select(param, method))
case Term.Select(select @ Term.Select(_, _), method) =>
View gist:31933b32214658c72e2e582c2ee05ecf
$ cat
record A(int x, int y) { }
$ javac エラー: レコードはプレビュー機能であり、デフォルトで無効になっています。
record A(int x, int y) { }
$ javac --enable-preview
エラー: --enable-previewは-sourceまたは--releaseとともに使用する必要があります
You can’t perform that action at this time.