在 Scala 中实现“.clone" [英] Implementing '.clone' in Scala

查看:83
本文介绍了在 Scala 中实现“.clone"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何在 Scala 中.clone我自己的对象.

这是用于模拟,因此必须具有可变状态,因此需要克隆.在提前模拟时间之前,我将克隆整个状态结构.

这是我目前的尝试:

抽象特征 Cloneable[A] {//似乎我们不能声明复制构造函数的原型//protected def this(o: A)//由类本身定义def myClone= new A(this)}类 S(var x: String) 扩展 Cloneable[S] {def this(o:S)= this(o.x)//for 'Cloneable'def toString= x}对象 TestX {val s1= new S("say, aaa")println(s1.myClone)}

一个.为什么以上不编译.给:

<前>错误:需要类类型但找到 Adef myClone= new A(this)^

B.有没有办法在特征中声明复制构造函数(def this(o:A)),以便使用该特征的类显示需要提供一个.

c.说abstract trait有什么好处吗?

最后,有没有更好的标准解决方案来解决所有这些问题?

我研究了 Java 克隆.似乎不是为了这个.Scala copy 也不是 - 它仅适用于案例类,它们不应该具有可变状态.

感谢您的帮助和任何意见.

解决方案

Traits 不能定义构造函数(而且我不认为 abstract 对 trait 有任何影响).

是否有任何理由需要使用复制构造函数而不仅仅是实现克隆方法?可能不必在类上声明 [A] 类型,但我至少声明了一个 self 类型,以便编译器确保该类型与类匹配.

trait DeepCloneable[A] { self: A =>def deepClone: A}class Egg(size: Int) extends DeepCloneable[Egg] {def deepClone = 新蛋(大小)}对象主扩展应用程序{val e = 新蛋(3)打印(e)println(e.deepClone)}

http://ideone.com/CS9HTW

I'm trying to figure out how to .clone my own objects, in Scala.

This is for a simulation so mutable state is a must, and from that arises the whole need for cloning. I'll clone a whole state structure before moving the simulation time ahead.

This is my current try:

abstract trait Cloneable[A] {
  // Seems we cannot declare the prototype of a copy constructor
  //protected def this(o: A)    // to be defined by the class itself

  def myClone= new A(this)
}

class S(var x: String) extends Cloneable[S] {
  def this(o:S)= this(o.x)    // for 'Cloneable'
  def toString= x
}

object TestX {
  val s1= new S("say, aaa")
  println( s1.myClone )
}

a. Why does the above not compile. Gives:

error: class type required but A found
  def myClone= new A(this)
                   ^

b. Is there a way to declare the copy constructor (def this(o:A)) in the trait, so that classes using the trait would be shown to need to provide one.

c. Is there any benefit from saying abstract trait?

Finally, is there a way better, standard solution for all this?

I've looked into Java cloning. Does not seem to be for this. Also Scala copy is not - it's only for case classes and they shouldn't have mutable state.

Thanks for help and any opinions.

解决方案

Traits can't define constructors (and I don't think abstract has any effect on a trait).

Is there any reason it needs to use a copy constructor rather than just implementing a clone method? It might be possible to get out of having to declare the [A] type on the class, but I've at least declared a self type so the compiler will make sure that the type matches the class.

trait DeepCloneable[A] { self: A =>
    def deepClone: A
}

class Egg(size: Int) extends DeepCloneable[Egg] {
    def deepClone = new Egg(size)
}

object Main extends App {
    val e = new Egg(3)
    println(e)
    println(e.deepClone)
}

http://ideone.com/CS9HTW

这篇关于在 Scala 中实现“.clone"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆