I have some authentication code, and today I had what I thought was the
bright idea of breaking behaviors into traits. But I have a couple
places in my code where I create a User as a var, then call convenience
methods on the case class which basically do a copy().
If I have everything in my User case class, this works fine. But if I
break the class into a User which dynamically extends traits, I get an
error with the copy:
case class User(name:String = "me")
trait HasPassword {
this: User =>
}
trait RequiresEmailValidation {
this: User =>
}
var u = new User() with HasPassword with RequiresEmailValidation
u = u.copy(name = "someone else")
/home/nolan/Projects/Bazaar/trunk/test.scala:13: error: type mismatch;
found : this.User
required: this.User with this.HasPassword with
this.RequiresEmailValidation
u = u.copy(name = "someone else")
^
one error found
Is there any way around this?
I wouldn't mind making User a val, but there are places where I
dynamically initialize some value based on another (if there are 0 users
saved in the database then I add the "admin" role to this first one),
but I don't know a clean way of doing that in case class initialization,
so I store the user in a var and initialize it a bit more before saving.
Thanks.