Type mismatch when using higher quality types

The library has a class with a higher type that takes one type parameter. I want to give it a type that takes two type parameters, so I use an expression type

to fix the other parameter.

But this is not what I expect.

The code boils down to this:

object Main {

  class Bar[T[_]] {
    def bar[A]: Option[T[A]] = None
  }

  def foo[A] = {
    type T[B] = Map[A, B]
    new Bar[T]
  }

  val f: Option[Map[String, Int]] = foo[String].bar[Int]

}

      

I am getting an error while compiling (Scala 2.11.4):

test.scala:12: error: type mismatch;
 found   : Option[T[Int]]
    (which expands to)  Option[scala.collection.immutable.Map[A,Int]]
 required: Option[Map[String,Int]]
  val f: Option[Map[String, Int]] = foo[String].bar[Int]
                                                   ^
one error found

      

Why is there a type error?

+3


source to share


1 answer


The labmdas type should help:

  class Bar[T[_]] {
    def bar[A]: Option[T[A]] = None
  }

  def foo[A] = {
    new Bar[({type M[B] = Map[A, B]})#M]
  }

  val f: Option[Map[String, Int]] = foo[String].bar[Int]

      



However, I cannot answer why type T does not work in this case.

+5


source







All Articles