In Scala, the following are possible:
scala> val l = List l: scala.collection.immutable.List.type = scala.collection.immutable.List$@7960c21a scala> l ( 1, 2, 3 ) res0: List[Int] = List(1, 2, 3)
In other words, Scala has a higher order polymorphism. I would like to use a higher order polymorphism to do the following.
sealed abstract class A { def eval () : A } case class A0 () extends A { ... } case class A1 ( a : A ) extends A { ... } case class A2 ( a : A, b : A ) extends A { ... } ....
So, I have a bunch of case classes, subclasses of A , whose constructors do not necessarily take the same number of arguments. I would also like to have a βgenericβ case class, something like this:
case class ApplyA ( c : ???, l : List [ A ] ) extends A { def eval () : A = { ??? } }
The idea is that ApplyA takes as its first argument a constructor for what is a subtype of A , and a list of arguments. Then, the eval method creates a possible class with a constructor (i.e., the List has the correct length) and returns it (this corresponds to l ( 1, 2, 3) in the List example above). What will be the argument type of the first constructor for ApplyA ?
This should be possible with a higher polymorphism, but I could not figure out how to do this. I know that I can do this even without using a higher order polymorphism by simply wrapping the constructors in functions and then passing those functions as the first argument to the constructor for ApplyA , but I would like to understand how to use the higher order polymorphism directly.
Martin berger
source share