Is it possible to refer to the types of Scala case class constructor arguments?

StackOverflow https://stackoverflow.com/questions/8681785

  •  06-05-2021
  •  | 
  •  

Question

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())?

Was it helpful?

Solution

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)

OTHER TIPS

case class Input[T](value: T)

trait MagicTrait[T,U] {
   val input1: Input[T]
   val input2: Input[U]
   def run: Unit
}

case class Foo(input1: Input[Int], input2: Input[String]) 
    extends MagicTrait[Int, String] {
  def run = println(input1.value * 2 + input2.value.toUpperCase)
}

scala> val m: MagicTrait[_,_] = Foo(Input(3), Input("hi"))
m: MagicTrait[_, _] = Foo(Input(3),Input(hi))

scala> m.run
6HI

edit:

If you want to find the types of the class parameters you can use the fact that case classes extend Product:

scala> Foo(2, "hi").productIterator.map(_.asInstanceOf[AnyRef].getClass).toList
res13: List[java.lang.Class[_]] = 
         List(class java.lang.Integer, class java.lang.String)

But this uses the reflection you wanted to avoid. This is why we use parameterization.

If you want to return its companion object, I'm not sure you can do this in a useful, type-safe way in the context of case classes, because companion objects don't extend an interface that specifies their extractor methods. You might be able to do something with structural types but it's probable that there's a better way to approach whatever problem it is that you're trying to solve.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top