Re: Type hinting and null values

103 views
Skip to first unread message

rktoomey

unread,
Jun 1, 2012, 10:23:20 AM6/1/12
to scala-salat
Hi Manuel,

Your problem has nothing to do with type hinting.

Null and empty fields are not persisted, so you need to supply default
arguments. (See http://docs.scala-lang.org/sips/completed/named-and-default-arguments.html
if you want a good explanation of how named and default args work in
Scala).

To supply null as your default argument, then just:

// when declaring a case class, you don't have to say "val" here - it
is already a val unless you say otherwise
case class AdministrativeArea(
name: String = null, // override here is not necessary since you
didn't define a value in the trait NezasaModelObject
country: String = null,
/* etc etc etc */) extends Area {
// rest omitted
}

Using null is less idiomatic to Scala than using Option so you may
want to look at that as another way of handling fields with optional
values. In that case, you would supply None as the default argument,
not null.

That should sort your problem.

Best,
Rose

On Jun 1, 5:49 am, hiltym <manuel.hi...@gmail.com> wrote:
> I've got the problem that 'null' members in my case classes are not written
> to the database if type hinting is used. When I try to read the objects
> back from the database, I get an error because of missing fields.
>
> Is that the default behavior of am I doing something wrong?
>
> Thanks for any support,
> Manuel
>
> ----
> Example: I have the following trait:
>
> @Salat
> trait Area extends NezasaModelObject {
>   val name: String
>   def contains(location: GeoLocation): Boolean
>
> }
>
> and a case class implementing it:
>
> case class AdministrativeArea(
>     override val name: String,
>     val country: String,
>     val state: String,
>     val county: String,
>     val town: String,
>     val district: String,
>     val neighborhood: String) extends Area {
> // rest omitted
>
> }
>
> Whenever my enclosing case class expects an AdministrativeArea, I get it
> written to the database with null values. Whenever my enclosing case class
> expects an Area, those attributes are completely missing and I cannot
> serialize it back...
Reply all
Reply to author
Forward
0 new messages