Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

generics - Scala: "Parameter type in structural refinement may not refer to an abstract type defined outside that refinement"

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:

error: Parameter type in structural refinement may not refer to an abstract type defined outside that refinement
    def >>[B](a: C[B])(implicit m: Monad[C]): C[B] = {
        ^

What am I doing wrong here?

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

IMPORTANT: This is NOT a question about Monads, it's a question about scala polymorphism in general.

EDIT: Here is my Monad definition

trait Monad[C[_]] {
  def >>=[A, B](a: C[A], f: A => C[B]): C[B]
  def >>=[B](a: C[B]): C[B]
  def apply[A](a: A): C[A]
}

BTW: I'm using scala 2.8RC1

Regards, raichoo

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...