Skip to content

Instantly share code, notes, and snippets.

@k4200
Created April 28, 2011 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k4200/946845 to your computer and use it in GitHub Desktop.
Save k4200/946845 to your computer and use it in GitHub Desktop.
inheritance or mix-in?
// There's a class called MappedPassword in Lift. I wanted to create a class that
// holds the plain text when a password is set.
//-------------------------------
// This works, but I didn't like the idea for two reasons.
// 1. The class name is long.
// 2. Seems kinda ugly to me.
abstract class MappedPasswordWithPlainWhenSet[A<:Mapper[A]](override val fieldOwner: A)
extends MappedPassword[A](fieldOwner) {
var plain: String = ""
override def set(value: String): String = {
plain = value
super.set(value)
}
}
// In a model class (User)
override lazy val password = new MyPassword2(this)
protected class MyPassword2(obj: User)
extends MappedPasswordWithPlainWhenSet(obj) {
override def displayName = fieldOwner.passwordDisplayName
}
//-------------------------------
// I wanted to make it a trait, but the following code doesn't work.
trait HoldsPlainWhenSet[A<:Mapper[A]] {
self: MappedPassword[A] =>
var plain: String = ""
override def set(value: String): String = {
plain = value
// self.super.set(value) // This doesn't compile.
// self.set(value) // This causes an infinite loop.
}
}
// In a model class (User)
override lazy val password =
new MyPassword(this) with HoldsPlainWhenSet[User]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment