Scala:“结构细化中的参数类型可能不涉及在该细化之外定义的抽象类型” [英] Scala: "Parameter type in structural refinement may not refer to an abstract type defined outside that refinement"

查看:127
本文介绍了Scala:“结构细化中的参数类型可能不涉及在该细化之外定义的抽象类型”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Scala泛型时遇到问题。虽然我在这里定义的第一个函数似乎完全没问题,但编译器会抱怨第二个定义:

  error:参数类型在结构细化中可能不涉及在该细化之外定义的抽象类型
def>> [B](a:C [B])(隐式m:Monad [C]):C [B] = {
^

我在这里做错了什么?

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

重要提示:这不是一个关于Monads的问题,这是一个关于scala多态性的问题。



编辑:这是我的Monad定义



<$ (A:C [A],f:A => C [A],B = B]:C [B]
def>> = [B](a:C [B]):C [B]
def apply [A](a:A):C [A]
}

顺便说一句:我正在使用scala 2.8RC1



问候,
raichoo

解决方案

填写示例中的空白,我做了这样的编译:

  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 ])(隐含的m:Monad [C]):C [B] = {
m>> = f
}
def>> [B] B])(隐含的m:Monad [C]):C [B] = {
m>> a



隐式def liftToMonad [A](c:C [A])= new D
}


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

解决方案

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
}

这篇关于Scala:“结构细化中的参数类型可能不涉及在该细化之外定义的抽象类型”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆