Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .tupled method when companion object is in class

I am in the process of migrating from Slick to Slick 2, and in Slick 2 you are meant to use the tupled method when projecting onto a case class (as shown here http://slick.typesafe.com/doc/2.0.0-RC1/migration.html)

The problem is when the case class has a companion object, i.e. if you have something like this

case class Person(firstName:String, lastName:String) {

}

Along with a companion object

object Person {
  def something = "rawr"
}

In the same scope, the tupled method no longer works, because its trying to run tupled on the object, instead of the case class.

Is there a way to retrieve the case class of Person rather than the object, so you can call tupled properly?

like image 542
mdedetrich Avatar asked Mar 13 '14 01:03

mdedetrich


4 Answers

You can also write

(Person.apply _).tupled

to avoid repeating the types.

like image 157
Alexey Romanov Avatar answered Nov 05 '22 14:11

Alexey Romanov


This is very similar to what Alexey Romanov said, but in order to avoid lifting apply whenever you need tupled, we just add it to our companion objects.

object Person {
  def something = "rawr"
  def tupled = (Person.apply _).tupled
}

Now you can call Person.tupled just like you would have if it didn't have a companion object.

like image 30
Tim Gautier Avatar answered Nov 05 '22 14:11

Tim Gautier


One workaround is define a companion object as follows:

object Person extends((String,String) => Person) {
    ...
}

See. https://groups.google.com/d/msg/scala-user/jyWBMz5Qslw/Bryv4ftzRLgJ

like image 28
kawty Avatar answered Nov 05 '22 14:11

kawty


To build on some of the other comments you could do the following as well since tuple is calling the generated default apply method for the case class.

object Person {
  ...
  def tupled = (this.apply _).tupled
}
like image 45
ekrich Avatar answered Nov 05 '22 14:11

ekrich