装饰实例的新实例

发布于 2024-10-10 04:13:29 字数 1843 浏览 0 评论 0原文

我有一组装饰器特征(为了简单起见,这里只有特征 T)混合在类(这里是 A 的子类)中。

trait T { def i: Int }
abstract class A
type AT = A with T
class B extends A
// class C extends A
// class D extends A
// ...
Now I have an instance a of e.g. class B which I handle as an instance of type AT (together with other A-subclass-AT-instances in a Seq[AT]).
val a: AT = new B with T { val i = 8 }
How can I generically decorate like this:
def toAT(a: A, i: Int): AT = {
 // how to ???
}
An obvious solution I thought about is:
trait T { def i: Int }
abstract class A {
  def asAT(i: Int): AT
}
class B extends A {
  def asAT(ii: Int): AT = new B with T { val i = ii }
}
type AT = A with T
def toAT(a: A, i: Int): AT = a.asAT(i)
But I don´t want to pollute my classes B, C, ..., Z with a new method and there are other decorations, so for every decoration-combo I need a new method for all subclasses! Is there a more generic way?

编辑: 为什么我需要 toAT 方法,但仍然使用上面的 redecorated 方法的明显解决方案方法的示例:

trait T { def b: Boolean}
type AT = A with T
abstract class A(val i: Int) {
  def changedBy(x: Int): A
  def redecorated(oldAT: AT): AT
}
class B(x: Int) extends A(x) {
  def changedBy(x: Int): A = new B(i * x) 
  def redecorated(oldAT: AT): AT = new B(i) with T { val b = oldAT.b }
}
class C(x: Int) extends A(x) {
  def changedBy(x: Int): A = new C(i * x * x) 
  def redecorated(oldAT: AT): AT = new C(i) with T { val b = oldAT.b }
}
val b = new B(1) with T { val b = true  }
val c = new C(1) with T { val b = false }
val x = 8
val res: Seq[AT] = Seq(b,c) map { at => at.changedBy(x).redecorated(at) }

I have a set of decorator-traits (for simplicity here only trait T) mixing in classes (here subclasses of A).

trait T { def i: Int }
abstract class A
type AT = A with T
class B extends A
// class C extends A
// class D extends A
// ...


Now I have an instance a of e.g. class B which I handle as an instance of type AT (together with other A-subclass-AT-instances in a Seq[AT]).

val a: AT = new B with T { val i = 8 }


How can I generically decorate like this:

def toAT(a: A, i: Int): AT = {
 // how to ???
}


An obvious solution I thought about is:

trait T { def i: Int }
abstract class A {
  def asAT(i: Int): AT
}
class B extends A {
  def asAT(ii: Int): AT = new B with T { val i = ii }
}
type AT = A with T
def toAT(a: A, i: Int): AT = a.asAT(i)


But I don´t want to pollute my classes B, C, ..., Z with a new method and there are other decorations, so for every decoration-combo I need a new method for all subclasses!
Is there a more generic way?

EDIT:
An example why I need the toAT method, but still with the obvious solution approach from above in terms of a redecorated method:

trait T { def b: Boolean}
type AT = A with T
abstract class A(val i: Int) {
  def changedBy(x: Int): A
  def redecorated(oldAT: AT): AT
}
class B(x: Int) extends A(x) {
  def changedBy(x: Int): A = new B(i * x) 
  def redecorated(oldAT: AT): AT = new B(i) with T { val b = oldAT.b }
}
class C(x: Int) extends A(x) {
  def changedBy(x: Int): A = new C(i * x * x) 
  def redecorated(oldAT: AT): AT = new C(i) with T { val b = oldAT.b }
}
val b = new B(1) with T { val b = true  }
val c = new C(1) with T { val b = false }
val x = 8
val res: Seq[AT] = Seq(b,c) map { at => at.changedBy(x).redecorated(at) }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

塔塔猫 2024-10-17 04:13:29

基本上,你做不到。

一种替代方法是使用 自动代理插件,作者:凯文赖特。另请参阅此处此处了解更多信息。

Basically, you can't do it.

One alternative is to use the auto proxy plugin, by Kevin Wright. See also here and here for more information about it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文