val originalA: A = // ... val update: Update = // ... val newA: A = A( id = originalA.id, a = update.a.getOrElse(originalA.a), ... )
All case classes have a copy method.
http://docs.scala-lang.org/tutorials/tour/case-classes.html (find the copy term)
val a = A(1, "", "", "") val update = Update(None, "scalaz".some, None) val b = a.copy( b = update.b.getOrElse(a.b) )
Also check out the lens pattern for copying deeply nested objects in a functional manner:
Once you have objects made up of other objects and so on, it becomes highly cumbersome to use the
copy method. Scalaz's lens pattern implementation is a great alternative.
- Scala case class copy with optional values
- Scala copy case class with generic type
- Scala case class copy doesn't always work with `_` existential type
- Scala case class copy constructor with dynamic fields
- Scala case class copy with dynamic named parameter
- Scala case class copy only with some parameters at runtime?
- How to get Scala case class fields and values as (String, String) with Shapeless or Macro
- Do two copies of a case class use twice the memory when only one property is changed, or does Scala reuse immutable values on copy to save memory?
- Scala case class copy method with Map parameter
- Scala - copy case class with dynamic fields. Is it possible?
- Scala generic case class with optional field
- Why Scala case class copy method parameterised only with the variables defined in the case class?
- How to read optional json values in case class using scala combinators
- Using Spark converting nested json with optional fields to Scala case class not working
- Compare a list values with case class using Scala and Spark
- How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration
- Scala case class extending Product with Serializable
- case class copy 'method' with superclass
- Case Classes with optional fields in Scala
- Scala 2.10 reflection, how do I extract the field values from a case class, i.e. field list from case class
- Read CSV in Scala into case class instances with error handling
- Scala wont pattern match with java.lang.String and Case Class
- Scala case class uses shallow copy or deep copy?
- Derived Scala case class with same member variables as base
- Scala spark: how to use dataset for a case class with the schema has snake_case?
- Replacing case class inheritance with extractors preserving exhaustiveness checks in Scala
- Problem with bounded type parameterised case class and default args in Scala
- How do I add a no-arg constructor to a Scala case class with a macro annotation?
- Scala case class with function parameters
- compare case class fields with sub fields of another case class in scala
More Query from same tag
- Can you call an object method dynamically off class parameterization in Scala?
- Transforming/repacking the results of a Slick query
- How to compile a scala program without any builder?
- scala code completion in eclipse
- Extracting schema from XSD File in scala: scalaxb
- How to check if the iterator in the for expression is a val or var?
- Implement abstract behaviour just once... trait as contract, abstract class as concrete-helper
- Akka Http - multiroutes and simple reflection of code with implicits
- scala.MatchError: null while conversion to JSON
- How to use scala for a backend?
- Connector Version for Spark - Cassandra 3.x
- Does this Scala Perl/Python architecture make sense
- Scala split Array by comma and each item by quotes
- indexing of large text files line by line for fast access
- How to use spark with existing Hadoop 2.x
- Convert JsValue to a model via Reads[T] which consits parts of list of arbitrary tuples and parts of known objects
- Unit test for Scala object (not class)
- Scala - the return using a for() or foreach() in a list
- Scala Map implementation keeping entries in insertion order?
- Why version of SBT is playing role in name of fully qualified dependency?
- Transition from thread model to actors
- How to convert string that contains only characters and numbers in scala?
- Change the format of file path which is partitioned by java.sql.Timestamp
- how to generate spark time series data
- spark scala: extracting xml from one column
- Combining elements in a Scala list
- How to write efficient type bounded code if the types are unrelated in Scala
- scala, Passing an operator as argument of a function
- how to give `x._1._2` parameters actual names
- Why does connection close automatically after 1 minute using Akka TCP?