Note that there are some explanatory texts on larger screens.

plurals
  1. POcase class copy 'method' with superclass
    primarykey
    data
    text
    <p>I want to do something like this:</p> <pre class="lang-scala prettyprint-override"><code>sealed abstract class Base(val myparam:String) case class Foo(override val myparam:String) extends Base(myparam) case class Bar(override val myparam:String) extends Base(myparam) def getIt( a:Base ) = a.copy(myparam="changed") </code></pre> <p>I can't, because in the context of getIt, I haven't told the compiler that every Base has a 'copy' method, but copy isn't really a method either so I don't think there's a trait or abstract method I can put in Base to make this work properly. Or, is there?</p> <p>If I try to define Base as <code>abstract class Base{ def copy(myparam:String):Base }</code>, then <code>case class Foo(myparam:String) extends Base</code> results in <code>class Foo needs to be abstract, since method copy in class Base of type (myparam: String)Base is not defined</code></p> <p>Is there some other way to tell the compiler that all <code>Base</code> classes will be case classes in their implementation? Some trait that means "has the properties of a case class"?</p> <p>I could make Base be a case class, but then I get compiler warnings saying that inheritance from case classes is deprecated?</p> <p>I know I can also:</p> <pre class="lang-scala prettyprint-override"><code>def getIt(f:Base)={ (f.getClass.getConstructors.head).newInstance("yeah").asInstanceOf[Base] } </code></pre> <p>but... that seems very ugly.</p> <p>Thoughts? Is my whole approach just "wrong" ?</p> <p><strong>UPDATE</strong> I changed the base class to contain the attribute, and made the case classes use the "override" keyword. This better reflects the actual problem and makes the problem more realistic in consideration of Edmondo1984's response.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload