How to use type constraint with abstract type

Given the following code:

trait S { type T } case class A(t: Seq[String]) extends S { type T = Seq[String] } 

I do not understand this compilation error: It seems that the evidence is not being used.

 def f[S<:A, X](g: => Seq[X])(implicit ev: S#T =:= Seq[X]) = new A(g) <console>:50: error: type mismatch; found : Seq[X] required: Seq[String] def f[S<:A, X](g: => Seq[X])(implicit ev: S#T =:= Seq[X]) = new A(g) 
+6
source share
1 answer

How silly it might look, replacing the order of the operands =:= solves the problem

 def f[S<:A, X](g: => Seq[X])(implicit ev: Seq[X] =:= S#T) = new A(g) 

Thanks scalac.

Explanation

When you say implicit ev S#T =:= Seq[X] , the compiler provides an implicit conversion from S#T to Seq[X] . However, it does not provide an equivalent conversion from Seq[X] to S#T and what is the stupid part: is this not an equality that should be commutative?

Further information on this subject is here: http://typelevel.org/blog/2014/07/02/type_equality_to_leibniz.html

+9
source

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


All Articles