【问题标题】:Scala 2.7 : reference is ambiguous (imported twice)Scala 2.7:引用不明确(导入两次)
【发布时间】:2010-04-30 09:40:41
【问题描述】:

在 Scala 2.7 中,我想将一个方法用作同一类的另一个方法的参数。

我有一个类和对象是同伴:

class mM(var elem:Matrix){
    //apply a function on a dimension rows (1) or cols (2) 
    def app(func:Iterable[Double]=>Double)(dim : Int) : Matrix = {
        ...
    }
    //utility function
    def logsumexp(): Double = {...}
}

object mM{
    def apply(elem:Matrix):mM={new mM(elem)}
    def logsumexp(elem:Iterable[Double]): Double ={
         this.apply(elem.asInstanceOf[Matrix]).logsumexp()
    }
}

通常我使用这样的 logsumexp mM(matrix).logsumexp,但如果想将它应用于我不能使用 mM(matrix).app(mM.logsumexp)(1) 的行,我会收到错误:

error: reference to mM is ambiguous;
it is imported twice in the same scope by
import mM
and import mM

什么是最优雅的解决方案?我应该将 logsumexp() 更改为另一个类吗?

谢谢,=)

【问题讨论】:

  • 您使用的是 Scala 2.7 吗?我在 2.8 下测试过,效果很好。
  • 确实,我使用的是 Scala 2.7 =)
  • 这并没有解决最初的问题,但是为什么你要使用asInstanceOfIterable[Double] 转换为Matrix 而不是简单地将Matrix 作为参数?
  • 很抱歉,我刚刚下载了 2.7 并测试了您的代码,它仍然有效。所以,肯定有其他问题。您能否提供一个完整程序且不可编译的最小示例?

标签: scala ambiguous


【解决方案1】:

我终于在我的 mM Class 中只使用了new mM(matrix) 而不是mM(matrix) 语句并避免了错误。

@Rex Kerr:你说得对,我把类型改成了Matrix

谢谢

【讨论】:

    猜你喜欢
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    相关资源
    最近更新 更多