0

In my codebase I used tags to encode some information into types. As tags works using t.asInstanceOf[T @@ U] I could get away with many troubles with writing mapping between wrapped and unwrapped values.

Recently I hit the wall with tagged types as they break some functionality in shapeless that I would like to use, so I am trying to use value classes.

So far I could cheat away some e.g. boolean checks with tagged types using something like:

implicit class TaggedBooleanAsFirstOperand[P1, U](val c: Rep[P1 @@ U]) {
    private val em = new BooleanColumnExtensionMethods[P1](c.asInstanceOf[Rep[P1]])
    type o = OptionMapperDSL.arg[Boolean, P1]

    def @&&[P2, R](b: Rep[P2])(implicit om: o#arg[Boolean, P2]#to[Boolean, R]): Rep[R] = em.&&[P2, R](b)
    def @||[P2, R](b: Rep[P2])(implicit om: o#arg[Boolean, P2]#to[Boolean, R]): Rep[R] = em.||[P2, R](b)
    def unary_! : Rep[P1] = em.unary_!
  }

  implicit class TaggedBooleanAsSecondOperand[P1](val c: Rep[P1]) {
    private val em = new BooleanColumnExtensionMethods[P1](c)
    type o = OptionMapperDSL.arg[Boolean, P1]

    def &&@[P2, U, R](b: Rep[P2 @@ U])(implicit om: o#arg[Boolean, P2]#to[Boolean, R]): Rep[R] = em.&&[P2, R](b.asInstanceOf[Rep[P2]])
    def ||@[P2, U, R](b: Rep[P2 @@ U])(implicit om: o#arg[Boolean, P2]#to[Boolean, R]): Rep[R] = em.||[P2, R](b.asInstanceOf[Rep[P2]])
  }

  implicit class TaggedBooleanAsBothOperands[P1, U](val c: Rep[P1 @@ U]) {
    private val em = new BooleanColumnExtensionMethods[P1](c.asInstanceOf[Rep[P1]])
    type o = OptionMapperDSL.arg[Boolean, P1]

    def @&&@[P2, V, R](b: Rep[P2 @@ V])(implicit om: o#arg[Boolean, P2]#to[Boolean, R]): Rep[R] = em.&&[P2, R](b.asInstanceOf[Rep[P2]])
    def @||@[P2, V, R](b: Rep[P2 @@ V])(implicit om: o#arg[Boolean, P2]#to[Boolean, R]): Rep[R] = em.||[P2, R](b.asInstanceOf[Rep[P2]])
  }

With AnyVal however, I cannot simply cast one type into the other where I want to, and I would have to provide some honest code allowing to achieve what I want.

I tried to read Slick source code, but the whole idea where should I start is a little blurry to me - relevant information are spread across many places, so I cannot simply pinpoint a few places to e.g. figure how to map one Rep into the other or extract data from wrapped and unwrapped Boolean and combine them.

Can some Slick experts recommend some good starting point for those?

1 Answer 1

0

Tags actually give you not only type safety. It gives you abstraction level as well. Once you wrapped your boolean into a tag you abstract away from runtime representation of your boolean. BTW you should use Tag.unwrap or Tag.unsubst instead of asInstanceOf. Slick works on a lower level of abstraction. It works with simple types which SQL is aware of. Of course you need to unwrap tags somewhere in between before working with DB. I would encapsulate it in table schema. See Mapped Tables section: http://slick.lightbend.com/doc/3.0.0/schemas.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.