4

在 C# 中,您可以显式地实现接口。然后只能通过将接口作为其静态类型的变量来调用显式实现的方法。这允许您避免名称/返回类型冲突,并根据this.

例如:

interface IFoo
{
    int DoSomething();
}

interface IBar
{
    string DoSomething();
}

class Impl : IFoo, IBar
{
    int IFoo.DoSomething() { /* Implementation for IFoo */ }
    string IBar.DoSomething() { /* A different implementation for IBar */ }
    public void DoSomething() { /* Yet another implementation for Impl */ }
}

您将如何在 Scala 中处理这种情况:

trait Foo {
    def doSomething(): Int
}

trait Bar {
    def doSomething(): String
}

class Impl extends Foo with Bar {
    /* only one "doSomething()" visible here (that of Bar?) */
    /* what now... ? */
}
4

2 回答 2

3

如果您只是想让您的类遵循两个独立的不兼容接口,则必须改为编写包装器。例如,

implicit case class ImplAsFoo(impl: Impl) extends Foo {
  def asFoo = this
  def doSomething: Int = 5
}

现在你可以

impl.asFoo

在使用站点切换到 Foo 包装。

但是,在某些情况下,使用类型类模式来提供可插入功能可能更自然:

trait IFoo[A] { def doSomething: Int }
trait IBar[A] { def doSomething: String }

// These need to be companions so :paste if you're using REPL
class Impl { def doSomething { println("Hey!") } }
object Impl {
  implicit object FooImpl extends IFoo[Impl] { def doSomething = 5 }
  implicit object BarImpl extends IBar[Impl] { def doSomething = "salmon" }
}

def needsFoo[A <: Impl: IFoo](a: A) = implicitly[IFoo[Impl]].doSomething

scala> needsFoo(new Impl)
res1: Int = 5

scala> (new Impl).doSomething
Hey!

它不完全相同,但这也解决了在没有命名方案的情况下实现不同的问题。(如果您需要doSomething使用该impl对象,您可以将其作为implicit object处理该情况的参数传递。)

如果你已经有了特质,那么这当然对你没有帮助。但是,当您从头开始设计时,您可能会尝试使用类型类,而不是拥有一堆具有不兼容方法的特征。

最后,如果你忍不住将一堆无类型的东西混在一起,你需要从中挑选Foos,你必须发明更精细的方案,如下所示:

trait CanBeFoo { def asFoo: Foo }
trait Foo { def doSomething: Int }

// :paste these two together
class Impl extends CanBeFoo {
  def doSomething { println("Ho!") } 
  def asFoo = ImplAsFoo(this)
}
case class ImplAsFoo(impl: Impl) extends Foo {
  def doSomething = 6
}

val myList = List("salmon", new Impl, new Foo { def doSomething = 4 })
def doIt(f: Foo) { println(f.doSomething) }
myList.foreach {
  case f: Foo => doIt(f)
  case cf: CanBeFoo => doIt(cf.asFoo)
  case _ => println("nuh-uh")
}

// Produces
// nuh-uh
// 6
// 4

您可能更喜欢中间地图:

myList.map{ case cf: CanBeFoo => cf.asFoo; case x => x }.foreach{
  case f: Foo => println(f.doSomething)
  case _ => println("nuh-uh")
}
于 2013-07-16T17:51:32.230 回答
2

不,这些是严格不兼容的基本类型。当一个类扩展一个特征时,它将该特征作为其类型标识的一部分。它不能像任何类一样具有相互不兼容的类型标识extends Foo with Bar(正如您编写这些特征)。

于 2013-07-16T16:11:18.810 回答