- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
What is wrong with this code? (Syntax error)
Sat, 2011-12-03, 19:57
package shared
import shared.Util.he
trait Copyable extends Cloneable {
def copy():Object={
var clone:Object=null
try {
clone=this.clone()
return clone
} catch {
case t:Throwable=>{
he(t,"Copyable copy")
return null
}
}
}
@throws(classOf[CloneNotSupportedException])
protected def clone():Object={
super.clone()
}
}
Scala IDE is not showing an error, but adding this to the project
causes Scala IDE to shut down in Java files. (not able to detect Scala
imports in Java files). It causes Scala IDE to shutdown, one of the
many things to do so, like xml errors.
> trait Copyable extends Cloneable {
> def copy():Object={
> var clone:Object=null
> try {
> clone=this.clone()
> return clone
> } catch {
> case t:Throwable=>{
> he(t,"Copyable copy")
> return null
> }
> }
> }
> @throws(classOf[CloneNotSupportedException])
> protected def clone():Object={
> super.clone()
> }
> }
The only thing "wrong" with this code is that the `override` modifier is
missing for the `clone` method. Apart from that, do you really think it
is necessary to override this method? You might want to consider using
case classes, they bring a type-safe `copy` method.