Scala: “Parameter type in structural refinement may not refer to an abstract type defined outside that refinement”

前端 未结 2 965
感情败类
感情败类 2020-12-11 21:40

I\'m having a problem with scala generics. While the first function I defined here seems to be perfectly ok, the compiler complains about the second definition with:

2条回答
  •  囚心锁ツ
    2020-12-11 22:04

    Filling in the blanks in your example, I made this compile:

    trait Monad[C[_]] {
      def >>=[A, B](f: A => C[B]): C[B]
      def >>[B](a: C[B]): C[B]
    }
    
    trait Lifter[C[_]] {
      class D {
        def >>=[A, B](f: A => C[B])(implicit m: Monad[C]): C[B] = {
          m >>= f
        }
        def >>[B](a: C[B])(implicit m: Monad[C]): C[B] = {
          m >> a
        }
      }
    
      implicit def liftToMonad[A](c: C[A]) = new D
    }
    

提交回复
热议问题