English 中文(简体)
在Schala的“几乎是”关系
原标题:"Is almost a" relationships in Scala
  • 时间:2011-11-11 06:54:55
  •  标签:
  • scala
  • mixins

I m试图设计一个等级等级等级,并配以类似等级,不构成“一种”关系。 请打电话<代码>Model。 这些班级意在与使用<代码>Model的类似算法相配,但要求并不相同。 请打电话<代码>战略。 陷阱是<代码>。 战略类别要求从<代码>Model类别中提取许多相同内容,但并非所有<代码>Model类别都能够采用这些必要方法。 我不想有空洞的“麻烦”方法,而这种方法只会扔下<条码>。 无支持的Exception,而是有一种基于类型的安全混合处理方法——我是否可以适用某种设计模式?

例如,

object Main extends App {
    trait A {
        def f(one: Int): Int
        def g(two: Int): Int
        def h(three: Int): Int
    }

    class A1 extends A {
        override def f(one: Int): Int = {one + 1}
        override def g(two: Int): Int = {two + 2}
        override def h(three: Int): Int = {assert(false); 0}
    }

    class A2 extends A {
        override def f(one: Int): Int = {assert(false); 0}
        override def g(two: Int): Int = {two - 2}
        override def h(three: Int): Int = {three - 3}
    }

    trait B {
        def combine(i: Int): Int
    }

    trait B1 extends B {
        this: A =>
        override def combine(i: Int) = {f(i) + g(i)}
    }

    trait B2 extends B {
        this: A =>
        override def combine(i: Int) = {g(i) + h(i)}
    }

    override def main(args: Array[String]): Unit = {
        val a11 = new A1 with B1
        val a22 = new A2 with B2

        println(a11.combine(3))
        println(a22.combine(3))

        val a12 = new A1 with B2
        println(a12.combine(3))
    }
}

www.un.org/Depts/DGACM/index_french.htm 战略类别。 <代码>A1的通知不能执行<代码>h(,A2可能无法执行f(>>,视战略类别而定,这可能不会成为问题。 因此,我能够发现,A的执行能够与B的执行形成协调,并及时加以执行。

I ve 曾使用自行制作的表示比通常延伸的“is a”关系多得多。

最佳回答

You could also use structural types, the classes don t even need to be related in their hierarchy:

class A1 {
  def f(i: Int) = {i + 1}
  def g(i: Int) = {i + 2}
}

class A2 {
  def g(i: Int) = {i * 2}
  def h(i: Int) = {i * i}
}

type FnG = { def f(i: Int): Int; def g(i: Int): Int}

class B {
  def combine1(a: FnG, i: Int) = a.f(i) + a.g(i)
  def combine2(a: { def g(i: Int): Int; def h(i: Int): Int}, i: Int) = 
      a.g(i) + a.h(i)
}

val a1 = new A1
val a2 = new A2
val b = new B
println(b combine1(a1, 3))
println(b combine2(a2, 3))

trait C {
  def combine(i: Int): Int
}
trait C1 extends C {
  this: FnG =>
  def combine(i: Int) = f(i) + g(i)
}
trait C2 extends C {
  this: { def g(i: Int): Int; def h(i: Int): Int} =>
  def combine(i: Int) = g(i) + h(i)
}

val newA1 = new A1 with C1
val newA2 = new A2 with C2

println(newA1 combine(3))
println(newA2 combine(3))

因此,你只需要具体说明(针对海峡)基类支持特定方法,而对于班级支持特定方法。 你们不需要承担任何等级。

问题回答

在此,我的解决办法是:

trait F { def f(one: Int): Int }
trait G { def g(two: Int): Int }
trait H { def h(three: Int): Int }

trait A
trait A1 extends A with F with G {
  def f(one: Int): Int = { one + 1 }
  def g(two: Int): Int = { two + 2 }
}
trait A2 extends A with G with H {
  def g(two: Int): Int = { two - 2 }
  def h(three: Int): Int = { three - 3 }
}

trait B {
  def combine(i: Int): Int
}
trait B1 extends B {
  this: A with F with G =>
  def combine(i: Int) = { f(i) + g(i) }
}
trait B2 extends B {
  this: A with G with H =>
  def combine(i: Int) = { g(i) + h(i) }
}

val a11 = new A1 with B1
val a22 = new A2 with B2

println(a11.combine(3))
println(a22.combine(3))

val a12 = new A1 with B2 // won t compile as you wanted




相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签