All frameworks
ANJUNAR / OPEN SOURCE

Scala Reflect

Type metadata generated at compile time for the JVM and Scala.js.

01

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.

02

What it does.

01

Generated descriptors

Capture class names, properties, constructors, base types and annotations at compile time.

02

Typed property access

Generate getters and setters from selectors rather than looking up fields reflectively.

03

Cross-platform metadata

Use the same descriptor model in JVM and Scala.js applications.

03

Installation

Add to your project
// JVM
libraryDependencies += "com.anjunar" %% "scala-reflect" % "1.1.2"

// Scala.js
libraryDependencies += "com.anjunar" %%% "scala-reflect" % "1.1.2

Package: com.anjunar:scala-reflect

04

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.

Source code
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.

Source code
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
05

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.

Your next step

Make it part of your stack.

Built carefully with Scala JS UI.Imprint