Search code examples
scalaintellij-ideafunctional-programming

Type mismatch in Scala with IDEA and FP


I am a rookie in Scala Programming, is there anyone can explain this, why the IDEA show these errors but works well?

case class FlatMap[A, B](sub: IO[A], f: A => IO[B]) extends IO[B]

@annotation.tailrec
  def run[A](io: IO[A]): A = io match {
    case Pure(a) => a
    case Suspend(r) => r()
    case FlatMap(sub, k) => sub match {
      case Pure(a) => run(k(a `Type mismatch, Required Nothing, Found Any`))
      case Suspend(r) => run(k(r() `same as above`))
      case FlatMap(y, g) => run(y flatMap (a => g(a `same as above`) flatMap k `Required Any => IO[NotInferredB], Found: Nothing => IO[A]`))
    }
  }

And I want to know more about the Functional programming and the Monad in Scala,

is there any good resources? Thanks a lot!


Solution

  • Fixed by specific the case type will be work well, like:

    io match {
        case Pure(a: A) => a
        case Suspend(r: () => A) => r()
        case FlatMap(sub: IO[A], k: A => IO[B]) => //...
    }