How to test type conformance of higher-kinded types in Scala

后端 未结 2 1646
情歌与酒
情歌与酒 2021-02-04 14:40

I am trying to test whether two \"containers\" use the same higher-kinded type. Look at the following code:

import scala.reflect.runtime.universe._

class Funct[         


        
2条回答
  •  终归单人心
    2021-02-04 15:17

    The following is an answer that works for the example I have given (and might help others), but does not apply to my (non-simplified) case.

    Stealing from @pedrofurla's hint, and using type-classes:

    trait ConfTest[A,B] {
      def conform: Boolean
    }
    
    trait LowPrioConfTest {
      implicit def ctF[A,B] = new ConfTest[A,B] { val conform = false }
    }
    
    object ConfTest extends LowPrioConfTest {
      implicit def ctT[A,B](implicit ev: A <:< B) =
        new ConfTest[A,B] { val conform = true }
    }
    

    And add this to Foo:

    def imp[B[_]](implicit ct: ConfTest[A,Funct[B,_]]) =
      println(ct.conform)
    

    Now:

    x.imp[Option] // --> true
    x.imp[List]   // --> false
    

提交回复
热议问题