Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Stacking traits, case classes and copy()
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Nolan Darilek  
View profile  
 More options Nov 14 2012, 2:12 pm
From: Nolan Darilek <no...@thewordnerd.info>
Date: Wed, 14 Nov 2012 13:11:46 -0600
Local: Wed, Nov 14 2012 2:11 pm
Subject: Stacking traits, case classes and copy()
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Som Snytt  
View profile  
 More options Nov 14 2012, 8:13 pm
From: Som Snytt <som.sn...@gmail.com>
Date: Wed, 14 Nov 2012 17:13:32 -0800
Local: Wed, Nov 14 2012 8:13 pm
Subject: Re: [scala-user] Stacking traits, case classes and copy()

On Wed, Nov 14, 2012 at 11:11 AM, Nolan Darilek <no...@thewordnerd.info>wrote:

> 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().

I think this is a similar question about abstracting over a case class copy:
http://stackoverflow.com/questions/12370244/case-class-copy-method-wi...

I don't think my answer there (which no one liked) has anything to do with
what I just came up with, but:

package userland

trait Userland {
  import language.implicitConversions
  trait Named { def name: String }
  trait Authd { def password: String }
  type User <: Named
  def user(n: String): User
  trait Aliaser {
    def aka(n: String): User
  }
  implicit def aliaser(u: User): Aliaser

}

trait Secureland { this: Userland =>
  type User = AuthUser
  case class AuthUser(name: String, password: String) extends Named with
Authd
  override def user(n: String) = AuthUser(n, "...")
  class AuthAliaser(val u: AuthUser) extends Aliaser {
    def aka(n: String) = u.copy(name=n)
  }
  override def aliaser(u: User) = new AuthAliaser(u)

}

object Test extends App with Userland with Secureland {
  val u = user("Bob")
  val v = u aka "Admin"
  println(v)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »