Filter on custom type doesn't compile

1,015 views
Skip to first unread message

Michael Pollmeier

unread,
Jun 19, 2014, 12:53:18 AM6/19/14
to scala...@googlegroups.com
I defined a custom type Status and can use it fine for insert and update, but not on filter. What am I missing?
I'm on Scala 2.10.3 and Slick 2.0.2.

The compiler says:
Error typechecking MappedTo expansion: class Status is abstract; cannot be instantiated
Error typechecking MappedTo expansion: class type required but Test.Foo.Two.type found


object Test {
  import scala.slick.driver.MySQLDriver.simple._
 
  object Foo {
    sealed abstract class Status(val value: String) extends MappedTo[String]
    object One extends Status("one")
    object Two extends Status("two")

    implicit val statusColumnType =
      MappedColumnType.base[Status, String](_.value, _ match {
        case One.value ⇒ One
        case Two.value ⇒ Two
      })
  }

  case class Foo(id: Long, status: Foo.Status)
  class FooT(tag: Tag) extends Table[Foo](tag, "Foo") {
    import Foo._

    def id = column[Long]("id")
    def status = column[Status]("status")

    def * = (id, status) <> ((Foo.apply _).tupled, Foo.unapply)
  }

  implicit val mockSession: scala.slick.jdbc.JdbcBackend.SessionDef = ???
  val tq = TableQuery[FooT]
  tq.map(_.status).update(Foo.Two) //compiles fine
  tq.filter(_.status === Foo.Two)  //doesn't compile

}                                                  

Thank you!

Christopher Vogt

unread,
Jun 20, 2014, 2:24:06 PM6/20/14
to scala...@googlegroups.com
Does

import Foo._
tq.filter(_.status === Foo.Two)

solve the problem?

Chris

Michael Pollmeier

unread,
Jun 22, 2014, 5:49:46 PM6/22/14
to scala...@googlegroups.com
That leads us to ambiguous implicits:

ambiguous implicit values:
[error]  both value BooleanColumnCanBeQueryCondition in object CanBeQueryCondition of type => scala.slick.lifted.CanBeQueryCondition[scala.slick.lifted.Column[Boolean]]
[error]  and value BooleanOptionColumnCanBeQueryCondition in object CanBeQueryCondition of type => scala.slick.lifted.CanBeQueryCondition[scala.slick.lifted.Column[Option[Boolean]]]
[error]  match expected type scala.slick.lifted.CanBeQueryCondition[Nothing]
[error]   tq.filter(_.status === Foo.Two)


I tried to selectively import from MySQLDriver.simple but couldn't get all the explicits I needed:
  import scala.slick.driver.MySQLDriver.simple.{ TableQuery, MappedColumnType, MappedTo, BaseColumnType, Table, Tag }
  import Foo._

could not find implicit value for evidence parameter of type slick.driver.MySQLDriver.BaseColumnType[String]
[error]       MappedColumnType.base[Status, String](_.value, _ match {
could not find implicit value for parameter tm: scala.slick.ast.TypedType[Long]
[error]     def id = column[Long]("id")
could not find implicit value for parameter tm: scala.slick.ast.TypedType[Test.Foo.Status]
[error]     def status = column[Status]("status")


I was hoping that this is quite a common case and I just missed something in my slick-newbieness.

Cheers
Michael

Norbert Schultz

unread,
Oct 28, 2015, 4:02:54 AM10/28/15
to Slick / ScalaQuery
Hi,

we faced the same problem.

However a simple type annotation should make it compile:

 
tq.filter(_.status === (Foo.Two : Foo.Status))

Regards,
Norbert

virtualeyes

unread,
Oct 28, 2015, 4:47:20 PM10/28/15
to Slick / ScalaQuery
The macro backing `MappedTo` requires a single value constructor; changing `object One extends ...` to `case object One extends ...` should fix the problem (not to mention removing the need for manually specifying an implicit mapper since, well, that's exactly what MappedTo provides for free ;-))
Reply all
Reply to author
Forward
0 new messages