【问题标题】:how can i extend a type to a trait in scala我如何将类型扩展到scala中的特征
【发布时间】:2012-03-15 14:41:46
【问题描述】:

我有一个用于鸭式打字的类型:

type t={
   def x:Int
 ...
}
class t2 {
 def x:Int=1
}
def myt:t=new t2 //ducktyping

我想编写一个强制接口类型的特征,但这不起作用:

trait c extends t { //interface DOES NOT COMPILE
  def x:Int=1
}

另一方面:如果我写一个 trait t1 而不是 type t 那么我就失去了鸭式打字功能:

trait t1 {
 def x:Int
}
type t=t1
trait c extends t1 { // t1 can be used as interface
  def x:Int=1
}
def myt:t=new t2  // DOES NOT COMPILE since t1 is expected

那么我怎样才能同时使用鸭式打字和接口呢?

【问题讨论】:

    标签: scala types duck-typing


    【解决方案1】:

    您只能在 Scala 中扩展类类实体(即类、特征、Java 接口)而不是一般类型(即结构类型、类型参数或成员)。但是,您可以自行输入到所有这些。这意味着我们可以重写你的非编译trait c如下,

    trait c { self : t =>
      def x : Int = 1
    }
    

    c 的主体内,this 的类型现在已知为t,即已知符合结构类型{ def x : Int },并且只能混合c进入一个确实符合该结构类型的类(通过直接实现签名,或者如果是抽象的,则通过重新声明自身类型并将义务传播到最终的具体类),

    type t = { def x : Int }
    
    trait c { self : t => }
    
    class t2 extends c {  // OK, t2 conforms to t
      def x : Int = 1
    }
    
    def myt : t = new t2  // OK, as before
    
    class t3 extends c {  // Error: t3 doesn't conform to c's self-type
      def y : String = "foo"
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-11
      • 1970-01-01
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      相关资源
      最近更新 更多