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

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

问题描述

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

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] = {
        ^

我在这里做错了什么?

   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
        }   
      }
    }

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

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

这是我的 Monad 定义

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]
}

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

BTW: I'm using scala 2.8RC1

问候,雷乔

推荐答案

在你的例子中填空,我做了这个编译:

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天全站免登陆