Implicit parameter resolution for higher kinded types

后端 未结 2 996
一向
一向 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:29

    That's because Some(42) is a more specific type than Option[Int]. It is a Some[Int]. See alternative coding below:

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

提交回复
热议问题