traits with immutable paramiters in scala

I want to make the following example so that Collar is immutable

  trait Collar{
    var text:String="";
  }

  class dog(val name:String){
    def bark()=  ...
  }

    val snoopy = new dog("snoopy") with Collar;
    snoopy.text="charley's dog";
    println(snoopy.text)

However when I try something like

  trait Collar(val text:String){
  }

I get the compile time error

traits or objects may not have parameters

Is there currently a a way to do this? If not, why not?

Remember, a good dog shouldn't care when it has a Collar.

Answers


Not at all sure what your real question is, but perhaps this is what you're looking for:

trait Collar{
  val tagText: String    // 'val', not 'var'; left undefined here
}

class Dog(val name:String) {
  def bark()=  ...
}

val snoopy = new Dog("snoopy") with Collar {
  val tagText= "charley's dog"
}
println(snoopy.tagText)

Alternatively, Collar.tagText can be set with an initial/default value AND also be overridden where Dog is instantiated.


Need Your Help

What controls when text in a <TD> line wraps?

html css table formatting

I have the following text in a &lt;TD&gt;

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.