Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这对我来说是一个常见的烦恼:
trait Foo { val x: Int } def foo(x: Int) = new Foo { val x = x // <-- Here }
在编写 Scala 代码时,我大概每 20 分钟就会遇到一次。
我知道的解决方案及其各自的缺陷是:
还有其他人吗?
我有时会使用这种俗气的方法:
def foo(x: Int): Foo = { val x1 = x new Foo { val x = x1 } }
这个怎么样?
trait Foo { val x: Int } object Foo { def apply(xt: Int) = new Foo { val x = xt } } def foo(xt: Int) = Foo(xt) // <-- Here