I am trying to create a custom field type in Squeryl. This field represents Isin code , so it is backed by a string field. Following the example in the documentation , I added a simple check before creating a new isin (it doesn't matter what the Isin code is or how it works):
trait Domain[A] { self: CustomType[A] => def validate(a: A): Unit validate(value) } class Isin(v: String) extends StringField(v) with Domain[String] { println("instantiating Isin") override def validate(s: String) { println("calling validate with " + s) assert(checkIsin(s)) } private def checkIsin(isin: String): Boolean = {
I added a few println to find out what happens. I use this field inside the model, for example
case class Asset( val id: Long = 0, val isin: Isin ) extends KeyedEntity[Long] object Asset { import Database.assets def create(isinCode: String) { inTransaction { assets.insert(new Asset(isin = new Isin(isinCode))) } } }
Now when I call Asset.create("US0378331005") (a valid ISIN), I get an exception. In stacktrace, it turns out that this exception is caused by invoking the init method with a null value, which is supposedly passed to checkIsin . Indeed, println operators print
calling validate with US0378331005 Instantiating Isin calling validate with
So it seems that the validate method is actually called twice, but the second time it gets null .
What is going wrong?
scala squeryl
Andrea Aug 29 '12 at 10:15 2012-08-29 10:15
source share