There is a way to create a Case Class using the tuple. Put another way: you can use a Case Class as a function to create values from tuples.
// simple demo case class
case class Klass(a: String, b: String) {
println(a + b)
}
// usage
// dot notation
Klass.tupled(("hello", "world"))
//infix notation
Klass tupled ("hello", "world")
// both calls output: helloworld
This means that you can use Case Class as a function:
val listWithTuples = ("hello","world") :: ("abra","kadabra") :: Nil
// infix notation
listWithTuples map (Klass tupled)
// output: helloworld
// abrakadabra
See the fiddle.
This works because:
- Case classes has the Companion Object, which is actually a FunctionN.
- Functions has the
.tupled
method, returning the function taking the arguments as a tuple.