Suppose there are two traits
trait LongKeyedRestricted[OwnerType <: LongKeyedRestricted[OwnerType]] {
self: => OwnerType
}
trait LongKeyed[OwnerType] {
self: => OwnerType
}
First trait definition is explicit that type parameter must be a subtype of LongKeyedRestricted[OwnerType]. However, given following definition of some User class
class User extends LongKeyed[User]
Am I not saying that User is a subtype of LongKeyed[User]. My question is how extra restriction in first trait definition is useful, I could not find a use case where this can be violated.
Reference Liftweb
trait LongKeyedMapper[OwnerType <: LongKeyedMapper[OwnerType]] extends KeyedMapper[Long, OwnerType] with BaseLongKeyedMapper {
self: OwnerType =>
}
Well... its like LongKeyedRestricted
ensures that its type parameter is a Sub-type of LongKeyedRestricted[ OwnerType ]
which ensures some properties which may be useful at some point.
To make sense of it all, lets consider following simpler example.
trait IntLike {
def me: Int
}
trait IntLikeHandle[ IntLike ] {
def myHandle: IntLike
}
trait IntLikeHandleStrict[ IntLike <: IntLikeHandle[ IntLike ] ] {
def myHandle: IntLike
}
case class IntLikeHandleConcrete( handle: IntLike )
extends IntLikeHandle[ IntLike ] {
def myHandle = handle
}
case class IntLikeHandleConcreteOther( handle: IntLike )
extends IntLike with IntLikeHandle[ IntLike ] {
def me = handle.me
def myHandle = handle
}
// Note :: ClassQualityDisclaimer : This class achieves nothing
// at all, and is just for demonstrating the possibilities.
case class IntLikeHandleStrictConcrete( handle: IntLikeHandleConcreteOther )
extends IntLikeHandleStrict[ IntLike ] {
def myHandle = handle.myHandle
// this class allows you to do some cool things which were not
// possible in class extending trait without restrictions.
// Note :: MethodQualityDisclaimer : This method achieves nothing
// at all, and is just for demonstrating the possibilities.
def me = math.max( handle.me, myHandle.me )
}
Note :: CodeQualityDisclaimer
: This code achieves nothing at all, and is just for demonstrating the possibilities.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.