in Education by
I'm struggling to get custom defined mapping between my case classes and database tables due to type inference problems. This is an example of what I'm getting, which is just an extremely trimmed down version from the original use case, but it should be enough to illustrate the problem: case class Example(id: Int, aString: String, optional: Option[Int], extra: String) class Examples(tag: Tag) extends Table[Example](tag, "EXAMPLE") { def id = column[Int]("ID", O.AutoInc, O.PrimaryKey) def aString = column[String]("A_STRING") def optional = column[Option[Int]]("AN_OPTIONAL") override def * = (id, aString, optional) <> (constructExample, extractExample) private def constructExample(id: Int, aString: String, optional: Option[Int]): Example = { Example(id, aString, optional, "MY EXTRA DATA") } private def extractExample(e: Example): Option[(Int, String, Option[Int])] = { Some((e.id, e.aString, e.optional)) } } Here I'm defining 2 custom functions to deal with the conversion between my case class and the database row. The problem is that the <> method is not able to infer the tuple types for constructing, giving the following error: Error:(60, 50) type mismatch; found : (Int, String, Option[Int]) => SlickExampleRepository.this.Example required: ? => ? override def * = (id, aString, optional) <> (constructExample, extractExample) In the Slick docs one can find the following: it can also be used with arbitrary mapping functions. In these cases it can be useful to call .shaped on a tuple on the left-hand side in order to get its type inferred properly. Otherwise you may have to add full type annotations to the mapping functions. There's no example for it, but I went ahead and tried the following: override def * = (id, aString, optional).shaped <> (constructExample, extractExample) This seems to only partially solve the problem, giving the following error: Error:(60, 57) type mismatch; found : (Int, String, Option[Int]) => SlickExampleRepository.this.Example required: ((Int, String, Option[Int])) => ? override def * = (id, aString, optional).shaped <> (constructExample, extractExample) So the final work-around I found for this was to change the signature of the constructExample function to receive a tuple and return an Example object, like so: private def constructExample(tuple: (Int, String, Option[Int])): Example = { Example(tuple._1, tuple._2, tuple._3, "MY EXTRA DATA") } But this is quite horrible and error prone, given that we're defining potentially quite long tuples and accessing its elements using ._1 and the like. Any hints on how to get this to work in a nice way? Many thanks JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You should use the following way to define your * projection: override def * = (id, aString, optional) <> ((constructExample _).tupled, extractExample) constructExample _ will convert your private method into a function, and .tupled would convert your function with 3 arguments into a function with one Tuple3 argument.

Related questions

0 votes
    I work with Spark often, and it would save me a lot of time if the compiler could ensure that a type is serializable. ... T to be serializable } It's not enough to constrain T...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    It's a sad fact of life on Scala that if you instantiate a List[Int], you can verify that your ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    I have a generic method which a generic type parameter T which is a subclass of MyClass. Inside that method, I want ... of type erasure): object Demo extends App { def myMethod[T...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I am working on a project with spark and scala and I am new to both but with lot of help from ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I have to retrieve Derived class objects stored in a Map given the respective class name as key. As ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    I have to retrieve Derived class objects stored in a Map given the respective class name as key. As ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    In scala, it is OK to convert a variable in the Seq, but if I construct the Seq with :: it ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    We are building sentiment analysis application and we converted our tweets dataframe to an array. We created another array ... .txt").getLines.toArray var happyCount=0 for (e...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    We are building sentiment analysis application and we converted our tweets dataframe to an array. We created another array ... .txt").getLines.toArray var happyCount=0 for (e...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I am working on spark project using Scala. I need to print each element of a list named 'c' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I have a 2 column (1 int and 1 double) dataframe "fit_comparison", of predicted values and linear ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    In Mercury I can use: A = B^some_field := SomeValue to bind A to a copy of B, except that ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    In Mercury I can use: A = B^some_field := SomeValue to bind A to a copy of B, except that ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    In Mercury I can use: A = B^some_field := SomeValue to bind A to a copy of B, except that ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    Definition says: RDD is immutable distributed collection of objects I don't quite understand what does it mean. Is ... one please help. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
...