我有一个对象,我需要将其类传递给一个带有java.lang.Class的注释,例如:
public @interface PrepareForTest {
Class<?>[] value()
}
object MyObject
@PrepareForTest(Array(?????))
class MySpec ...
Run Code Online (Sandbox Code Playgroud)
我试过了:
@PrepareForTest(Array(classOf[MyObject]))
// error: not found: type MyObject
@PrepareForTest(Array(MyObject))
// error: type mismatch
// found: MyObject.type (with underlying type object MyObject
// required: java.lang.Class[_]
@PrepareForTest(Array(classOf[MyObject.type]))
// error: class type required by MyObject.type found
Run Code Online (Sandbox Code Playgroud)
不知道还有什么可以尝试.
我正在尝试使用扩展语义实现一种SortedMap.我正在尝试将SortedMap委托为存储,但无法绕过方差约束:
class IntervalMap[A, +B](implicit val ordering: Ordering[A])
//extends ...
{
var underlying = SortedMap.empty[A, List[B]]
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误.我理解为什么我得到错误(我理解方差).我没有得到的是如何实现这种类型的委托.是的,B上的协方差是必需的.
error: covariant type B occurs in contravariant position in type scala.collection.immutable.SortedMap[A,List[B]] of parameter of setter underlying_=
Run Code Online (Sandbox Code Playgroud)