Scala:三重上下文界限,未找到证据参数

时间:2016-08-25 13:10:53

标签: scala context-bound

我已将此编辑为@Zhi Yuan Wang回答的问题的简单形式:

object ContBound { 
  def f2[A: Seq, B: Seq]: Unit = {
      val a1: Seq[A] = evidence$1
      val b2: Seq[B] = evidence$2 
     }

  def f3[A: Seq, B: Seq, C: Seq]: Unit = {
    val a1: Seq[A] = evidence$1
    val b2: Seq[B] = evidence$2
    val a3: Seq[C] = evidence$3      
  }   
}

我收到以下错误:

not found value evidence$1
not found value evidence$2
type mismatch; found :Seq[A] required: Seq[C]

尽管在REPL中获得以下内容:

 def f3[A: Seq, B: Seq, C: Seq]: Unit =
 |    {
 |       val a1: Seq[A] = evidence$1
 |       val b2: Seq[B] = evidence$2
 |       val a3: Seq[C] = evidence$3      
 |    }  
f3: [A, B, C](implicit evidence$1: Seq[A], implicit evidence$2: Seq[B], implicit evidence$3: Seq[C])Unit

Zhi的助手是对的。以下编译:

object ContBound { 
  def f2[A: Seq, B: Seq]: Unit = {
    val a1: Seq[A] = evidence$1
    val b2: Seq[B] = evidence$2 
  }

  def f3[A: Seq, B: Seq, C: Seq]: Unit = {
    val a1: Seq[A] = evidence$3
    val b2: Seq[B] = evidence$4
    val a3: Seq[C] = evidence$5      
  }   
}

但是我仍然没有将此视为正确行为,因为这些是两种不同方法的参数,通常允许方法重用参数名称。

1 个答案:

答案 0 :(得分:2)

你试过吗

def comma3[A: RParse, B: RParse, C: RParse, D](f: (A, B, C) => D): D =
    expr match {
       case CommaExpr(Seq(e1, e2, e3)) =>
           f(evidence$3.get(e1), evidence$4.get(e2), evidence$5.get(e3))
}

因为证据$ 1已经被

使用了
def comma3[]??