Domanda

I have a quite puzzling question. I am playing with squeryl, and found when I used:

package models

import org.squeryl.{Schema, KeyedEntity}

object db extends Schema {
  val userTable = table[User]("User")

  on(userTable)(b => declare(
      b.email is(unique,indexed("idxEmailAddresses"))
      ))
}

I had to import import org.squeryl.PrimitiveTypeMode._

But this does not make sense to me. Here is is defined in org.squeryl.dsl.NonNumericalExpression, but why do I have to include the seemingly irrelevant import org.squeryl.PrimitiveTypeMode._?

Thank you.

È stato utile?

Soluzione

I agree with @sschaef that this is due to required implicit conversions. When APIs (like squeryl) decide to build a DSL (domain specific language) in order to get a slick looking way to code against their API, implicit conversions will be required. The core API probably takes certain types of objects that it might be cumbersome/ugly to be instantiating directly in the code. Thus, they will use implicit conversions to do some of the lifting for you and keep the DSL as clean as possible. If you check out the Scaladoc for the PrimitiveTypeMode object, you can see the many implicit defs that are defined on it. Implicit conversions (used in pimping libraries) will 'upconvert' from one type into another to gain access to more functionality on the pimped out class. When the code is the implicit things are explicitly included into the final compiled code.

http://squeryl.org/api/index.html#org.squeryl.PrimitiveTypeMode$

Also, I believe the implicit conversion you are looking for is:

import org.squeryl.PrimitiveTypeMode.string2ScalarString

which is inherited from org.squeryl.dsl.QueryDsl.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top