Skip to content
Anjunar/ DOCS

Scala Reflect

Reflection without runtime reflection. Macros describe your types at compile time; at runtime you read descriptors and call generated accessors, on the JVM and in Scala.js alike.

1.1.4Current release
JVM ยท Scala.jsRuns on
Scala 3.3Language version
MITLicense

Installation

A cross-built artifact. With sbt 2, %% adds the right platform suffix for the JVM and for Scala.js.

sbt
libraryDependencies += "com.anjunar" %% "scala-reflect" % "1.1.4" // sbt 1, Scala.js: libraryDependencies += "com.anjunar" %%% "scala-reflect" % "1.1.4"

Describe a case class

reflect expands to a ClassDescriptor at compile time. The values below are read from it while this page renders.

descriptor.typeName
demo.Person
descriptor.simpleName
Person
descriptor.isCaseClass
true
descriptor.constructors.head.parameterNames
name, age
descriptor.getProperty("age").map(_.propertyType.typeName)
Some(scala.Int)
scala
package demo case class Person(name: String, age: Int) import reflect.macros.ReflectMacros val descriptor = ReflectMacros.reflect[Person]
01 / Compile time

Metadata from macros

The structure of a type is read by the compiler, not discovered at runtime. Nothing is guessed and nothing is missing on Scala.js.

02 / Typed access

Accessors instead of Method.invoke

Getters and setters are generated code. A read-only property gets no setter at all.

03 / One model

The same descriptors everywhere

Server and browser share one model of your types. scalajs-ui and json-mapper build on it.

Contents

From a descriptor to a registry

Describe types, access their properties, then register them to look them up by name at runtime.