Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3215732
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:12:43+00:00 2026-05-17T15:12:43+00:00

Say I have got following two case class es: case class Address(street: String, city:

  • 0

Say I have got following two case classes:

case class Address(street: String, city: String, state: String, zipCode: Int)
case class Person(firstName: String, lastName: String, address: Address)

and the following instance of Person class:

val raj = Person("Raj", "Shekhar", Address("M Gandhi Marg", 
                                           "Mumbai", 
                                           "Maharashtra", 
                                           411342))

Now if I want to update zipCode of raj then I will have to do:

val updatedRaj = raj.copy(address = raj.address.copy(zipCode = raj.address.zipCode + 1))

With more levels of nesting this gets even more uglier. Is there a cleaner way (something like Clojure’s update-in) to update such nested structures?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:12:44+00:00Added an answer on May 17, 2026 at 3:12 pm

    Zippers

    Huet’s Zipper provides convenient traversal and ‘mutation’ of an immutable data structure. Scalaz provides Zippers for Stream (scalaz.Zipper), and Tree (scalaz.TreeLoc). It turns out that the structure of the zipper is automatically derivable from the original data structure, in a manner that resembles symbolic differentiation of an algebraic expression.

    But how does this help you with your Scala case classes? Well, Lukas Rytz recently prototyped an extension to scalac that would automatically create zippers for annotated case classes. I’ll reproduce his example here:

    scala> @zip case class Pacman(lives: Int = 3, superMode: Boolean = false) 
    scala> @zip case class Game(state: String = "pause", pacman: Pacman = Pacman()) 
    scala> val g = Game() 
    g: Game = Game("pause",Pacman(3,false))
    
    // Changing the game state to "run" is simple using the copy method:
    scala> val g1 = g.copy(state = "run") 
    g1: Game = Game("run",Pacman(3,false))
    
    // However, changing pacman's super mode is much more cumbersome (and it gets worse for deeper structures):
    scala> val g2 = g1.copy(pacman = g1.pacman.copy(superMode = true))
    g2: Game = Game("run",Pacman(3,true))
    
    // Using the compiler-generated location classes this gets much easier: 
    scala> val g3 = g1.loc.pacman.superMode set true
    g3: Game = Game("run",Pacman(3,true)
    

    So the community needs to persuade the Scala team that this effort should be continued and integrated into the compiler.

    Incidentally, Lukas recently published a version of Pacman, user programmable through a DSL. Doesn’t look like he used the modified compiler, though, as I can’t see any @zip annotations.

    Tree Rewriting

    In other circumstances, you might like to apply some transformation across the entire data structure, according to some strategy (top-down, bottom-up), and based on rules that match against the value at some point in the structure. The classical example is transforming an AST for a language, perhaps to evaluate, simplify, or collect information. Kiama supports Rewriting, see the examples in RewriterTests, and watch this video. Here’s a snippet to whet your appetite:

    // Test expression
    val e = Mul (Num (1), Add (Sub (Var ("hello"), Num (2)), Var ("harold")))
    
    // Increment every double
    val incint = everywheretd (rule { case d : Double => d + 1 })
    val r1 = Mul (Num (2), Add (Sub (Var ("hello"), Num (3)), Var ("harold")))
    expect (r1) (rewrite (incint) (e))
    

    Note that Kiama steps outside the type system to achieve this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have two domain names: altcognito.com and say I've got the other following
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have two lists: var list1 = new int[] {1, 2, 3}; var
Let´s say I´ve got the following xml string: <Item Name=\$Resources:myresource,somestring;\</Item> Now I want to
Say I have the following abstract class: class AbstractClass { public: AbstractClass() {} virtual
Let's say that I have got two lists: temp<-c(con.sin.results,sin.results,exp.results) Temp<-c([,1:16],[,17:32],[,33:48],[,49:64]) Each of the variables
I've got the following design problem: Say I have 1 million of plain text
This question about Timers for windows services got me thinking: Say I have (and
I've got some financial data to store and manipulate. Let's say I have 2
I've got a grid view. I want it to say you have nothing to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.