This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Case class and companion Object

1 reply
Jesse Eichar
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.

Hi,

Is it not possible to have a case class and add methods to its
companion object? I understand that a companion object is generated
for the case class, which is fine but I would like to add methods to it.

Example:

case class Foo(name:String)
object Foo{
def apply()=new Foo("defaultname")
}

Jesse

DRMacIver
Joined: 2008-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Case class and companion Object
On Thu, Feb 26, 2009 at 2:46 PM, Jesse Eichar <jeichar.w@gmail.com> wrote:
Hi,

Is it not possible to have a case class and add methods to its companion object?  I understand that a companion object is generated for the case class, which is fine but I would like to add methods to it.

Example:

case class Foo(name:String)
object Foo{
       def apply()=new Foo("defaultname")
}

Yes, it is. It won't work in the interpreter though.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland