Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.Instant
- /**
- * Created by aloise on 01.09.16.
- */
- case class User( name:String, email:String, password:String, createdAt:Instant )
- class WithId[T <: Product ]( val id: Int, val value: T ) extends Product {
- override def productElement(n: Int): Any = n match {
- case 0 => id
- case _ => value.productElement( n - 1 )
- }
- override def productArity: Int = 1 + value.productArity
- override def canEqual(that: Any): Boolean = {
- that match {
- case value1: this.type =>
- value1.canEqual(this) && ( value1.id == this.id )
- case _ =>
- false
- }
- }
- }
- object WithIdHelper {
- class WithIdRichHelper[T<:Product](value:T) {
- def withId( id: Int ): WithId[T] = {
- new WithId[T](id, value)
- }
- }
- implicit def toProduct[T<:Product]( withIdClass:WithId[T] ):T = {
- withIdClass.value
- }
- implicit def liftToWithId[T<:Product](value:T):WithIdRichHelper[T] = {
- new WithIdRichHelper[T](value)
- }
- }
- object CaseClassTests {
- def main(args: Array[String]): Unit = {
- import WithIdHelper.{ toProduct, liftToWithId }
- val user = User("aloise","test", "password", Instant.now())
- val userWithId = user withId 5
- println( userWithId.id, userWithId.name, userWithId.email )
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement