2017-06-24 23 views
0

鉴于此真棒一块完全亲斯卡拉:斯卡拉 - 明确提及参数

trait SomethingCool { 
    def foo: Int => String 
} 

def makeSomethingCool(foo: Int => String = i => i.toString): SomethingCool = { 
    new SomethingCool { 
    override def foo = foo // method foo does nothing other than call itself recursively 
    } 
} 

在那里我有,有一个默认的实现一个性状的工厂方法,我怎么能是指从内部参数名new SomethingCool { }?它似乎被特质的功能名称所掩盖。

+1

难道你不能只是调用参数别的东西? – marstran

+0

我可以用某种“为什么没有想到这个”或者“这不是对变量名称范围规则问题的回答”,但我不会。相反,我会说“谢谢你的建议,我会记住这一点。” –

+0

哦,我并没有试图粗鲁或任何事情。对不起,如果你认为。 – marstran

回答

3

你不能。但这里有一个解决方法:

def makeSomethingCool(foo: Int => String = i => i.toString): SomethingCool = { 
    val _foo = foo 
    new SomethingCool { 
    override def foo = _foo 
    } 
}