The Scala 3 compiler (Dotty) providing advanced type inference, metaprogramming capabilities, and enhanced performance for modern Scala development
npx @tessl/cli install tessl/maven-org-scala-lang--scala3-compiler_3@3.7.0The Scala 3 compiler (Dotty) is a next-generation compiler for the Scala programming language featuring advanced type inference, improved metaprogramming capabilities through inline functions and macros, better error messages, and enhanced performance. It supports modern language features like union and intersection types, opaque type aliases, extension methods, and contextual abstractions.
build.sbt: libraryDependencies += "org.scala-lang" %% "scala3-compiler" % "3.7.0"import dotty.tools.dotc.{Main, Driver, Compiler}
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.interfaces._For plugin development:
import dotty.tools.dotc.plugins.{Plugin, StandardPlugin, PluginPhase}
import dotty.tools.dotc.core.Phases.PhaseFor interactive/IDE usage:
import dotty.tools.dotc.interactive.{InteractiveDriver, InteractiveCompiler}import dotty.tools.dotc.{Main, Driver}
import dotty.tools.dotc.interfaces.{SimpleReporter, CompilerCallback}
// Simple compilation using Main entry point
Main.main(Array("MyFile.scala", "-d", "output"))
// Programmatic compilation with custom settings
val driver = new Driver
val reporter = driver.process(Array("MyFile.scala", "-classpath", "lib/*"))
// Compilation with custom reporter and callback
val customReporter: SimpleReporter = diagnostic =>
println(s"${diagnostic.level}: ${diagnostic.message}")
val callback: CompilerCallback = new CompilerCallback {
override def onClassGenerated(source, generatedClass, className) =
println(s"Generated class: $className")
}
val result = driver.process(
Array("MyFile.scala", "-d", "classes"),
customReporter,
callback
)The Scala 3 compiler is built around several key components:
Main, Driver) for different compilation modes with customizable compilation processCompiler class managing phases and runs with context-based executionMegaPhase combining multiple mini-phases for efficiencyInteractiveDriver with incremental compilation supportPrimary compilation interfaces for batch compilation, programmatic usage, and build tool integration.
object Main extends Driver {
def main(args: Array[String]): Unit
}
class Driver {
def process(args: Array[String]): Reporter
def process(args: Array[String], reporter: Reporter | Null, callback: CompilerCallback | Null): Reporter
def setup(args: Array[String], rootCtx: Context): Option[(List[AbstractFile], Context)]
}
class Compiler {
def phases: List[List[Phase]]
def newRun(using Context): Run
def nextRunId: Int
def reset()(using Context): Unit
}Framework for creating compiler plugins that can extend the compilation pipeline with custom phases and transformations.
sealed trait Plugin {
def name: String
def description: String
def optionsHelp: Option[String]
}
trait StandardPlugin extends Plugin {
def initialize(options: List[String])(using Context): List[PluginPhase]
}
trait ResearchPlugin extends Plugin {
def init(options: List[String], plan: List[List[Phase]])(using Context): List[List[Phase]]
}IDE integration capabilities providing incremental compilation, context preservation, and interactive features for development tools.
class InteractiveDriver extends Driver {
def currentCtx: Context
// Interactive compilation methods for IDE features
}
class InteractiveCompiler extends Compiler {
// Compiler variant optimized for interactive usage
}External Java-compatible interfaces for tool integration and reporting, enabling seamless integration with Java-based build tools and IDEs.
trait SimpleReporter {
def report(diag: Diagnostic): Unit
}
trait CompilerCallback {
def onClassGenerated(source: SourceFile, generatedClass: AbstractFile, className: String): Unit = {}
def onSourceCompiled(source: SourceFile): Unit = {}
}
trait Diagnostic {
def level(): Int
def message(): String
def position(): Option[SourcePosition]
}Context system providing compilation state management, phase execution control, and configuration handling throughout the compilation pipeline.
object Contexts {
def ctx(using ctx: Context): Context
def inContext[T](c: Context)(inline op: Context ?=> T): T
def atPhase[T](phase: Phase)(inline op: Context ?=> T)(using Context): T
}REPL (Read-Eval-Print Loop) functionality for interactive Scala development and script execution capabilities.
class ReplDriver {
// REPL compilation and execution engine
}
object repl.Main {
def main(args: Array[String]): Unit
}
class ScriptEngine extends javax.script.ScriptEngine {
// JSR-223 ScriptEngine implementation
}