What are the limitations on inference of higher-kinded types in Scala?

后端 未结 2 723
渐次进展
渐次进展 2020-12-23 22:15

In the following simplified sample code:

case class One[A](a: A) // An identity functor
case class Twice[F[_], A](a: F[A], b: F[A]) // A functor transformer
         


        
2条回答
  •  悲哀的现实
    2020-12-23 23:00

    Note (3 years later, July 2016), scala v2.12.0-M5 is starting to implement SI-2172 (support for higher order unification)

    See commit 892a6d6 from Miles Sabin

    -Xexperimental mode now only includes -Ypartial-unification

    It follows Paul Chiusano's simple algorithm:

    // Treat the type constructor as curried and partially applied, we treat a prefix
    // as constants and solve for the suffix. For the example in the ticket, unifying
    // M[A] with Int => Int this unifies as,
    //
    //   M[t] = [t][Int => t]  --> abstract on the right to match the expected arity
    //   A = Int               --> capture the remainder on the left
    

    The test/files/neg/t2712-1.scala includes:

    package test
    
    trait Two[A, B]
    
    object Test {
      def foo[M[_], A](m: M[A]) = ()
      def test(ma: Two[Int, String]) = foo(ma) // should fail with -Ypartial-unification *disabled*
    }
    

    And (test/files/neg/t2712-2.scala):

    package test
    
    class X1
    class X2
    class X3
    
    trait One[A]
    trait Two[A, B]
    
    class Foo extends Two[X1, X2] with One[X3]
    object Test {
      def test1[M[_], A](x: M[A]): M[A] = x
    
      val foo = new Foo
    
      test1(foo): One[X3]     // fails with -Ypartial-unification enabled
      test1(foo): Two[X1, X2] // fails without -Ypartial-unification
    }
    

提交回复
热议问题