Scala Reflect
Type metadata generated at compile time for the JVM and Scala.js.
The idea
Scala Reflect generates class and property descriptors at compile time. Applications can inspect types and use typed accessors on the JVM or in Scala.js without depending on runtime reflection.
What it does.
Generated descriptors
Capture class names, properties, constructors, base types and annotations at compile time.
Typed property access
Generate getters and setters from selectors rather than looking up fields reflectively.
Cross-platform metadata
Use the same descriptor model in JVM and Scala.js applications.
Installation
// JVM libraryDependencies += "com.anjunar" %% "scala-reflect" % "1.1.2" // Scala.js libraryDependencies += "com.anjunar" %%% "scala-reflect" % "1.1.2
Package: com.anjunar:scala-reflect
Start building.
Generate a descriptor with ReflectMacros.reflect, inspect its properties, or request accessors when code needs to read and write values.
Inspect a generated descriptor
The macro records the structure of Person while compiling; the descriptor is available at runtime.
import reflect.macros.ReflectMacros case class Person(name: String, age: Int) val descriptor = ReflectMacros.reflect[Person] println(descriptor.simpleName) // Person println(descriptor.properties.map(_.name).mkString(", ")) val name = descriptor.getProperty("name").get println(name.propertyType.typeName) // ...String
Generate a typed accessor
A selector becomes a getter and setter without a runtime lookup by field name.
import reflect.macros.ReflectMacros case class Settings(var theme: String, version: Int) val theme = ReflectMacros.makeAccessor[Settings, String](_.theme) val settings = Settings("light", 1) println(theme.get(settings)) // light if (theme.hasSetter) theme.set(settings, "dark") println(settings.theme) // dark
By design.
Macros extract structure while compiling and store it in lightweight descriptors. Higher-level mapping, validation and UI code can use that metadata without runtime classpath scanning.