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 8841911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:50:04+00:00 2026-06-14T10:50:04+00:00

Is it possible to generically replace arguments in a case class? More specifically, say

  • 0

Is it possible to generically replace arguments in a case class? More specifically, say I wanted a substitute function that received a “find” case class and a “replace” case class (like the left and right sides of a grammar rule) as well as a target case class, and the function would return a new case class with arguments of the find case class replaced with the replace case class? The function could also simply take a case class (Product?) and a function to be applied to all arguments/products of the case class.

Obviously, given a specific case class, I could use unapply and apply — but what’s the best/easiest/etc way to generically (given any case class) write this sort of function?

I’m wondering if there is a good solution using Scala 2.10 reflection features or Iso.hlist from shapeless.

For example, what I really want to be able to do is, given classes like the following…

class Op[T]
case class From(x:Op[Int]) extends Op[Int]
case class To(x:Op[Int]) extends Op[Int]

case class Target(a:Op[Int], b:Op[Int]) extends ...
// and lots of other similar case classes

… have a function that can take an arbitrary case class and return a copy of it with any elements of type From replaced with instances of type To.

  • 1 1 Answer
  • 0 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-06-14T10:50:06+00:00Added an answer on June 14, 2026 at 10:50 am

    If you’ll pardon the plug, I think you’ll find that the rewriting component of our Kiama language processing library is perfect for this kind of purpose. It provides a very powerful form of strategic programming.

    Here is a complete solution that rewrites To‘s to From‘s in a tree made from case class instances.

    import org.kiama.rewriting.Rewriter
    
    class Op[T]
    case class Leaf (i : Int) extends Op[Int]
    case class From (x : Op[Int]) extends Op[Int]
    case class To (x : Op[Int]) extends Op[Int]
    
    case class Target1 (a : Op[Int], b : Op[Int]) extends Op[Int]
    case class Target2 (c : Op[Int]) extends Op[Int]
    
    object Main extends Rewriter {
    
        def main (args : Array[String]) {
            val replaceFromsWithTos =
                everywhere {
                    rule {
                        case From (x) => To (x)
                    }
                }
    
            val t1 = Target1 (From (Leaf (1)), To (Leaf (2)))
            val t2 = Target2 (Target1 (From (Leaf (3)), Target2 (From (Leaf (4)))))
    
            println (rewrite (replaceFromsWithTos) (t1))
            println (rewrite (replaceFromsWithTos) (t2))
        }
    
    }
    

    The output is

    Target1(To(Leaf(1)),To(Leaf(2)))
    Target2(Target1(To(Leaf(3)),Target2(To(Leaf(4)))))
    

    The idea of the replaceFromsWithTos value is that the rule construct lifts a partial function to be able to operate on any kind of value. In this case the partial function is only defined at From nodes, replacing them with To nodes. The everywhere combinator says “apply my argument to all nodes in the tree, leaving unchanged places where the argument does not apply.

    Much more can be done than this kind of simple rewrite. See the main Kiama rewriting documentation for the gory detail, including links to some more examples.

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

Sidebar

Related Questions

Is that generally possible? Is the console able to format a link so that
Possible Duplicate: Can main function call itself in C++? I found this problem very
I have an application that is used in image processing, and I find myself
I'm trying to make a function that can take an enum type, display all
Not sure if this is possible with a regular expression replace command, but I
I understand that if you have some function in a linq query that does
I'd like to be able to pass all the arguments received in my method
Is it possible to define it generically over all types which have a method
My main question: I know you can generically output class fields with reflection, even
How do a populate an NSArray const? Or more generically how can I fix

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.