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) // ...StringGenerate 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) // darkBy 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.
Make it part of your stack.
Explore further.
Scala.js UI
A Scala 3 and Scala.js UI framework for server-rendered applications.
↗Hibernate DDL Manager
Controlled schema evolution from Hibernate metadata.
↗JSON Mapper
JSON mapping for existing object graphs and domain models.
↗Scala Universe
A resolved view of JVM types, generics and annotations.
↗Scala JS Ember
A modular rich-text editor built on one immutable document model.
↗