Implicit parameter resolution for higher kinded types

后端 未结 2 997
一向
一向 2020-12-31 15:42

Consider the following code:

object foo {

    trait Bar[Q[_]]

    implicit object OptionBar extends Bar[Option]

    def test[T, C[_]](c: C[T])(implicit ba         


        
2条回答
  •  时光取名叫无心
    2020-12-31 16:32

    It's not going to work in all cases, but as stated, you can try this:

    object foo {
      trait Bar[Q[_]]
    
      implicit object OptionBar extends Bar[Option]
    
      def test[T, C[_], D](c: D)(implicit bar: Bar[C], ev: D <:< C[T]) = ()
    
      def main(args: Array[String]) {
        test(Some(42)) //???
      }
    }
    

    Interestingly, this doesn't infer, although it expresses the same thing:

    def test[T, C[_], D <: C[T]](c: D)(implicit bar: Bar[C]) = ()
    

    To learn more about <:<, see:

    • What do <:<, <%<, and =:= mean in Scala 2.8, and where are they documented?
    • <:< operator in scala

提交回复
热议问题