How does curly braces following trait instantiation work?

后端 未结 3 1290
旧时难觅i
旧时难觅i 2020-12-17 11:01

I find some confusing use of trait in some unittesting code, such as:

trait MyTrait {
  val t1 = ... //some expression
  val t2 = ... //some expression
}
         


        
3条回答
  •  被撕碎了的回忆
    2020-12-17 11:22

    val t = new MyTrait {
      val t1 = ... //some expression
      val t2 = ... //some expression
    }
    

    is the same as

    val t = new AnyRef with MyTrait {
      val t1 = ... //some expression
      val t2 = ... //some expression
    }
    

    is the same as

    val t = new Object with MyTrait {
      val t1 = ... //some expression
      val t2 = ... //some expression
    }
    

提交回复
热议问题