开发者

Scala Copy() Odd Behavior

I开发者_StackOverflow社区'm experiencing an odd bit of behavior when I use the auto-generated copy() method that was added in Scala-2.8.

From what I've read, when you declare a given class as a case-class, a lot of things are auto-generated for you, one of which is the copy() method. So you can do the following...

case class Number(value: Int)
val m = Number(6)

println(m)                     // prints 6

println( m.copy(value=7) )     // works fine, prints 7

println( m.copy(value=-7) )    // produces:  error: not found: value value

println( m.copy(value=(-7)) )  // works fine, prints -7

I apologize if this question has already been asked, but what is going on here?


Scala allows many method names that other languages don't, including =-. Your argument is being parsed as value =- 7 so it is looking for a method =- on value which doesn't exist. Your workaround all change the way the expression is parsed to split up the = and the -.

scala> var foo = 10
foo: Int = 10

scala> foo=-7
<console>:7: error: value =- is not a member of Int
       foo=-7
       ^
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜