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

  • Home
  • SEARCH
  • 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 7014337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:30:30+00:00 2026-05-27T22:30:30+00:00

My goal is to create a trait that a case class can extend, which

  • 0

My goal is to create a trait that a case class can extend, which can process each constructor argument and then pass them as arguments to a method of the case class. All of the constructor arguments will be the same type with different type parameters, and the method will take arguments that match the type parameter of each constructor argument. You can think of it as a pre-processor of the constructor arguments. For example,

case class Foo(input1:Input[Int], input2:Input[String]) extends MagicTrait {
  def run(input1:Int, input2:String) { ... }
}

Is this at all feasible? Is it feasible in a way that isn’t terribly ugly (e.g. all reflection)? Is it possible to refer to the companion object of a case class in a way that is at all generic across case classes (e.g. a function that takes the output of Companion.unapply())?

  • 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-05-27T22:30:30+00:00Added an answer on May 27, 2026 at 10:30 pm

    Seeing as an acceptable solution allows the preprocessing functionality to be moved off the instances to an associated object the main remaining difficulty is that you want to be able to abstract over the arity and types (ie. the shape) of your case class constructors. This is possible with the HList implementation and polymorphic function values from shapeless.

    First some preliminaries,

    import shapeless.HList._
    import shapeless.Functions._
    import shapeless.Poly._
    import shapeless.TypeOperators._
    
    // Implementation of your Input wrapper
    case class Input[T](value: T)
    
    // Value extractor as a shapeless polymorphic function value
    object value extends (Input ~> Id) {
      def default[T](i : Input[T]) = i.value
    }
    

    We can now define a preprocessor base class which provides an apply method which takes an HList of Input types, maps the value polymorphic function across it (ie. performing the preprocessing) and then passes the resulting HList of non-Input types to the provided case class constructor (which is given in hlisted form, see below),

    // Pre-processer base class
    abstract class Preprocessor[In <: HList, Out <: HList, R](ctor : Out => R)
      (implicit mapper : MapperAux[value.type, In, Out]) {
        def apply(in : In) = ctor(in map value)
      }
    

    Now we define the case class with the post-processing component types,

    case class Foo(input1 : Int, input2 : String)
    

    and add one line of boilerplate,

    object FooBuilder extends Preprocessor((Foo.apply _).hlisted)
    

    (here the Foo companion object factory method is provided as the Preprocessor constructor argument in HListed form as required above.)

    Now we can construct Foo instances using the FooBuilder.

    val foo = FooBuilder(Input(23) :: Input("foo") :: HNil)
    

    Unfortunately it isn’t (currently) possible to combine the FooBuilder object with the Foo companion object: if you attempt to have the Foo companion extend Preprocessor you’ll discover that the Foo factory method isn’t available to be passed as the Preprocessor constructor argument.

    To illustrate that this solution is really abstracting over type and arity, here’s how we might add a second differently shaped case class,

    case class Bar(input1 : Int, input2 : String, input3 : Boolean)
    
    object BarBuilder extends Preprocessor((Bar.apply _).hlisted)
    
    val bar = BarBuilder(Input(23) :: Input("foo") :: Input(true) :: HNil)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The goal is to create a mock class which behaves like a db resultset.
Can't get this to work. Goal is to create a overflowing scrollable div that
My goal is to create some kind of swing component that can do two
My goal is to create an eBook that I can read with the Mobipocket
My goal is to create a map of maps so that I can retrieve
High Level Goal: Create a single Maven Web Application project that can be used
The goal: To create a .NET dll i can reference from inside SQL Server
My goal is to create a reusable Attached Behavior for a FlowDocumentScrollViewer, so that
My goal is to create an animation with the YUI Animation Utility that does
My goal is to create a validated IP address field that will be validated

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.