Scala Implicit Generators

If I have an implicit from Ato B, how can I automatically get implicits from F[A]to F[B]?

For example, if I have implicit toInt[A](l: List[A]) = l.size, and now I want to have an implicit from (List[A], Int)to (Int, Int)that reuses an implicit toInt. Is this possible in Scala?

+4
source share
1 answer

Implicits can use other implicits to convert values. Therefore, given yours toInt:

implicit def toInt[A](l: List[A]): Int = l.size

2- Int, . (List[Int], Int) (Int, Int):

implicit def tupleConvert[A <% Int, C](x: (A, C)): (Int, C) = (x._1, x._2)

A <% Int , , implict A Int.

, :

implicit def tupleConvert2[A <% B, B, C](x: (A, C)): (B, C) = (x._1, x._2)

2- (A, C) (B, C) A B. - Scala implicits, *.

* ( , , SI-2046, SI-3340, )

+4

Source: https://habr.com/ru/post/1538330/


All Articles