How to create case class with default parameters in Scala

Often times I have a lot of case classes in the project and I need to write some generic code that will create them using default constructor parameters.

So here is how to create case class with default parameters using Scala reflection:

import scala.reflect.runtime.universe
import scala.reflect.internal.{Definitions, SymbolTable, StdNames}

object Main {
def newInstanceWithDefaultParameters(className: String): Any = {
val runtimeMirror: universe.Mirror = universe.runtimeMirror(getClass.getClassLoader)
val ds = universe.asInstanceOf[Definitions with SymbolTable with StdNames]
val classSymbol = runtimeMirror.staticClass(className)
val classMirror = runtimeMirror.reflectClass(classSymbol)
val moduleSymbol = runtimeMirror.staticModule(className)
val moduleMirror = runtimeMirror.reflectModule(moduleSymbol)
val moduleInstanceMirror = runtimeMirror.reflect(moduleMirror.instance)
val defaultValueMethodSymbols =
.filter("apply"), 1).toString.dropRight(1)))
val defaultValueMethods =
val primaryConstructorMirror = classMirror.reflectConstructor(classSymbol.primaryConstructor.asMethod)
primaryConstructorMirror.apply( _*)

def main(args: Array[String]): Unit = {
val instance = newInstanceWithDefaultParameters(classOf[MyClass].getName)

case class MyClass(a: Int = 1, b: String = "foo")

Source code on Github.

Подписаться на мой блог

Мои записки о программировании. Я пишу редко.

Оставьте свою почту, чтобы не пропустить новые статьи