mthaler / replica   0.0.5

Apache License 2.0 GitHub

Tiny library to create copies of case classes

Scala versions: 2.13 2.12 2.11

Build Status codecov.io Maven Central Scaladoc

replica is tiny library to create copies of case classes with updated values using reflection.

The main use case for this library is a flat class hierachy of case classes extending an abstract base class that defines some common fields. Case class copy methods are generated by the compiler and cannot be accessed from the base class. Replica offers a simple way to update values defined in a base class without writing boilerplate code for each case class.

Installation

replica is available from the Maven Central repository. The latest release is 0.0.2 and is built against Scala 2.11.7.

If you use SBT you can include replica in your project with

libraryDependencies += "com.mthaler" %% "replica" % "0.0.2"

Usage

import com.mthaler.replica.Replicator

abstract class HasName extends Product {

  def name: String
}
case class Person(name: String, age: Int) extends HasName
case class Pet(name: String) extends HasName

implicit class RichHasName[T <: HasName](value: T) {
  def withName(name: String) = Replicator.copy(value, Map("name" -> name))
}

Person("Richard Feynman", 42).withName("Paul Dirac")

The result is Person("Paul Dirac", 42).

##License replica is licensed under APL 2.0.