Reputation: 32290
So I have a tuple that I want to pass as the parameters for a case class in Scala. For case classes without type parameters, this is easy, as I can do:
scala> case class Foo(a: Int, b: Int)
defined class Foo
scala> (Foo.apply _)
res0: (Int, Int) => Foo = <function2>
scala> val tuple = (1, 2)
tuple: (Int, Int) = (1,2)
scala> res0.tupled(tuple)
res1: Foo = Foo(1,2)
scala> Foo.tupled(tuple)
res2: Foo = Foo(1,2)
However, if the case class has a type parameter, it doesn't seem to work:
scala> (Bar.apply _)
res26: (Nothing, Nothing) => Bar[Nothing] = <function2>
scala> res26.tupled(tuple)
<console>:18: error: type mismatch;
found : (Int, Int)
required: (Nothing, Nothing)
res26.tupled(tuple)
^
scala> (Bar[Int].apply _)
<console>:16: error: missing arguments for method apply in object Bar;
follow this method with `_' if you want to treat it as a partially applied function
(Bar[Int].apply _)
scala> Bar.tupled(tuple)
<console>:17: error: value tupled is not a member of object Bar
Bar.tupled(tuple)
^
scala> Bar[Int].tupled(tuple)
<console>:17: error: missing arguments for method apply in object Bar;
follow this method with `_' if you want to treat it as a partially applied function
Bar[Int].tupled(tuple)
^
How do I partially apply a case class with a type parameter? I'm using Scala 2.10.3.
Upvotes: 2
Views: 1731
Reputation: 7732
This appears to work:
scala> case class Foo[X](a:X, b: X)
defined class Foo
scala> Foo.apply[Int] _
res1: (Int, Int) => Foo[Int] = <function2>
scala>
Upvotes: 4