Why does this Scala implicit conversion fail when explicit conversion works?

后端 未结 2 1585
眼角桃花
眼角桃花 2021-01-14 02:51

Why doesn\'t the following implicit conversion work even though explicitly calling the function works?

scala> implicit def view[A, C](xs: C)(implicit ev:          


        
2条回答
  •  难免孤独
    2021-01-14 03:14

    Simply put, as the error message suggests, it cannot infer A. There's no information the compiler can use to infer what A must be, so it must assume it can be anything.

    You might retort that it can infer A from C <:< Iterable[A], but that would turn the problem on its head: instead of using <:< to check constraints, it would use the constraints to infer the type of A! That logic is not sound.

提交回复
热议问题