Scala 2.7:引用不明确(导入两次)

发布于 2024-08-30 11:50:22 字数 822 浏览 1 评论 0原文

在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() 更改为另一个类吗?

谢谢,=)

In Scala 2.7, I want to use a method as a parameter of another method of the same class.

I have a class and objects which are companions:

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

Normally I use logsumexp like this mM(matrix).logsumexp but if want to apply it to the rows I can't use mM(matrix).app(mM.logsumexp)(1), I get the error:

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

What is the most elegant solution ? Should I change logsumexp() to another class ?

Thanks,=)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰つ倒转 2024-09-06 11:50:23

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

@Rex Kerr:你是对的,我将类型更改为Matrix

谢谢

I finally only used new mM(matrix) and not mM(matrix) statements inside my mM Class and avoided the error.

@Rex Kerr : You are right I changed the type to Matrix.

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文