0

I'm very new to Scala, Play Framework and Squeryl. I already understand the concepts of val and var, but I'm having a hard time on trying to model my entities. As I saw on Squeryl documentation, sometimes they use var on id and other times use val. What is the best approach for id and other values(sometimes they use var/val and other times Option, last one is only for nullable fields on entities)?

Example 1

    class Playlist(var id: Long, 
                   var name: String, 
                   var path: String) extends KeyedEntity[Long] {
    }

Example 2

class Author(val id: Long, 
              val firstName: String, 
              val lastName: String,
              val email: Option[String]) {
    def this() = this(0,"","",Some(""))        
 }

And why sometimes they extend the KeyedEntity[T] and sometimes don't?

I really appreciate some help!

adheus
  • 3,985
  • 2
  • 20
  • 33

1 Answers1

1

In Squeryl 0.9.5, all entities needed to extend KeyedEntity[T] however with 0.9.6 you can provide the KeyedEntityDef implicitly. See this for an example.

Option[T] is used when the field can contain null values. When the field is null, None is returned.

As for val vs. var it is exactly as with any other class in Scala. var allows for reassignment whereas val is, more or less, read-only. If you are going to change values, a lot of people simply make the field a var. Alternately, if you are using a case class you can use copy to create a new object with updated values or you can update the value via reflection.

Community
  • 1
  • 1
jcern
  • 7,798
  • 4
  • 39
  • 47
  • Sir, could complete your question with an example of the last topic you mentioned so I can mark your answer as correct? It will help others if they find all information here at once(and it will help me too :] ). Thank you. – adheus Jan 20 '15 at 21:38