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 getC...

问题:

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):

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

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,

but I'm really not fond of having to cast the instances I'm testing for.

Is there anything I can do about it?


回答1:

beTheSameInstanceAs(CardSuit.HEART) returns a Matcher<CardSuit>, so it can't match an arbitrary ICardSuit. That makes sense (though Matcher could be contra-variant, you'd need covariance here). But you can:

  1. Call explicitly beTheSameInstanceAs<ICardSuit>(CardSuit.HEART).

  2. 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).

  3. Because beTheSameInstanceAs(x) really can match anything, declare an equivalent function which returns a Matcher<Any>:

    fun beTheSameInstanceAsAny(x: Any) = beTheSameInstanceAs(x)
    
    // usage
    "HEART" -> it should beTheSameInstanceAsAny(CardSuit.HEART)
    
  • 发表于 2019-03-12 23:13
  • 阅读 ( 180 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除