[英]How do you get rid of superfluous casts to implemented interface?
Let's say I have an interface 假设我有一个界面
public interface ICardSuit {
/**short name*/
public String getName();
/** the colour of this card*/
public ICardColour getColour();
}
that I decide to implement with an enum: 我决定用一个枚举实现:
public enum CardSuit implements ICardSuit {
HEART{
@Override
public ICardColour getColour() {
return CardColour.RED;
}
},
SPADE{
@Override
public ICardColour getColour() {
return CardColour.BLACK;
}
},
DIAMOND{
@Override
public ICardColour getColour() {
return CardColour.RED;
}
},
CLUBS {
@Override
public ICardColour getColour() {
return CardColour.BLACK;
}
}
;
@Override
public String getName() {
return this.name();
}
}
I now want to test it (using kotlintest because I'm developing a fondness for it): 现在,我想对其进行测试(使用kotlintest,因为我对此很喜欢):
class CardSuitTest : FunSpec(){
init {
test("there are exactly four suits"){CardSuit.values().size shouldBe 4}
test("suits implement interface"){CardSuit.values().forEach { it shouldBe instanceOf(ICardSuit::class) }}
test("suits have correct names"){
val suits = CardSuit.values() as Array<out ICardSuit>
suits.forEach { when(it.name){
"HEART" -> it should beTheSameInstanceAs(CardSuit.HEART as ICardSuit)
"SPADE" -> it should beTheSameInstanceAs(CardSuit.SPADE as ICardSuit)
"DIAMOND" -> it should beTheSameInstanceAs(CardSuit.DIAMOND as ICardSuit)
"CLUBS" -> it should beTheSameInstanceAs(CardSuit.CLUBS as ICardSuit)
} }
}
test("suits have correct colours"){
CardSuit.values().forEach { when(it){
CardSuit.HEART,CardSuit.DIAMOND -> it.colour shouldBe CardColour.RED
CardSuit.CLUBS, CardSuit.SPADE -> it.colour shouldBe CardColour.BLACK
} }
}
}
}
where I need to cast to ICardSuit
because if I do not, the compiler complains that 我需要转换为
ICardSuit
因为如果我不这样做,编译器会抱怨
None of the following functions can be called with the arguments supplied.
* T.should(Matcher<T>) where T cannot be inferred for infix fun <T> T.should(matcher: Matcher<T>): Unit defined in io.kotlintest.matchers
* ICardSuit.should((ICardSuit) → Unit) where T = ICardSuit for infix fun <T> T.should(matcher: (T) → Unit): Unit defined in io.kotlintest.matchers
I want to keep the as Array<out ICardSuit>
because it's the easiest way to make sure I only access interface properties, 我想保留
as Array<out ICardSuit>
因为这是确保我仅访问接口属性的最简单方法,
but I'm really not fond of having to cast the instances I'm testing for. 但是我真的不喜欢强制转换要测试的实例。
Is there anything I can do about it? 有什么我可以做的吗?
Is there a specific reason you need to use the matcher beSameInstanceAs
? 您是否需要使用匹配器
beSameInstanceAs
的特定原因?
You can do something like: 您可以执行以下操作:
val suits = CardSuit.values() as Array<out ICardSuite>
suits.forEach {
when (it.name) {
"HEART" -> it shouldBe CardSuit.HEART
"SPADE" -> it shouldBe CardSuit.SPADE
}
}
However, if you really want to use beSameInstanceAs
, you can: 但是,如果您确实要使用
beSameInstanceAs
,则可以:
suits.forEach {
when(it.name) {
"HEART" -> it shouldBeSameInstanceAs CardSuit.HEART
"SPADE" -> it shouldBeSameInstanceAs CardSuit.SPADE
}
}
I'm not really getting any complaints from the compiler here 我在这里并没有收到编译器的任何投诉
beTheSameInstanceAs(CardSuit.HEART)
returns a Matcher<CardSuit>
, so it can't match an arbitrary ICardSuit
. beTheSameInstanceAs(CardSuit.HEART)
返回Matcher<CardSuit>
,因此它不能匹配任意ICardSuit
。 That makes sense (though Matcher
could be contra -variant, you'd need covariance here). 这是有道理的(尽管
Matcher
可能是反变数的,在这里您需要协变)。 But you can: 但是你可以:
Call explicitly beTheSameInstanceAs<ICardSuit>(CardSuit.HEART)
. 明确调用
beTheSameInstanceAs<ICardSuit>(CardSuit.HEART)
。
Create a helper function 创建一个辅助函数
inline fun <T1, reified T2 : T1> Matcher<T2>.widen() = object : Matcher<T1>() { override fun test(value: T1) = if (value is T2) this.test(value) else Result(false, "$value is not a ${T2::class.name}", "$value is a ${T2::class.name}") }
and call 并打电话
it should beTheSameInstanceAs(CardSuit.HEART).widen()
(I think type inference should work here). (我认为类型推断应该在这里起作用)。
Because beTheSameInstanceAs(x)
really can match anything, declare an equivalent function which returns a Matcher<Any>
: 因为
beTheSameInstanceAs(x)
实际上可以匹配任何东西,所以声明一个等效函数,该函数返回Matcher<Any>
:
fun beTheSameInstanceAsAny(x: Any) = beTheSameInstanceAs(x) // usage "HEART" -> it should beTheSameInstanceAsAny(CardSuit.HEART)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.