I am new to Scala (came from the world of Ruby).
And I was curious about the concept of “traits” in Scala (which should be ~ like modules in ruby, if I understand it correctly).
And here is a precedent.
Suppose I have a class called User defined as follows:
class User { def password() : String = "generating a password (default)" }
And suppose I have a SecurePasswords feature in which I would like to "override" the password method defined in the User class.
trait SecurePasswords { def password() : String = "generating a secure password (non-default)" }
And suppose I want this to apply to instances of the User class, and not to the entire class.
val a = new User val b = new User with SecurePasswords a.password()
Now this is the ideal conclusion that I would expect, however, I get various errors, such as " anonymous class inherits conflicting members ... (Note: this can be resolved declaring etc etc ...) "
Can this be done in Scala, or am I asking / doing something really strange too much? Is it possible to do without any additional class definitions, e.g. UserWithSecurePassword extends User
Thank you all in advance!
PS If you are wondering “why?”, Just assume that the system will contain many objects that require a password (and possibly a secure password), so this symptom can be used in many places.
source share