小妹最近刚开始接触 scala
在此之前 有接触过java 但对 functional programming没有接触过
最近读到 scala的英文教学书里的 symbol literal 实在不是很了解
去爬了很多文发现他很像 ruby 里的 symbol
根据自己的理解 主要的概念 就是说 symbol可以自订identifier
(如果也什么错误或观念需要厘清的可以指证我 谢谢~~~)
但 scala书中提到
For instance, you might want to define a method that updates a record in a
database:
scala> def updateRecordByName(r: Symbol, value: Any) {
// code goes here
}
updateRecordByName: (Symbol,Any)Unit
The method takes as parameters a symbol indicating the name of a record
field and a value with which the field should be updated in the record. In
a dynamically typed language, you could invoke this operation passing an
undeclared field identifier to the method, but in Scala this would not
compile:
scala> updateRecordByName(favoriteAlbum, "OK Computer")
<console>:6: error: not found: value favoriteAlbum
updateRecordByName(favoriteAlbum, "OK Computer")
Instead, and almost as concisely, you can pass a symbol literal:
scala> updateRecordByName('favoriteAlbum, "OK Computer")
想请问各位大大
这边有点不懂QQ 为什么直接写favoriteAlbum会有错呢??
symbol完之后为何就没有错误呢?
附上pdf之网址 在5.2 p114 的地方
http://www.cs.ucsb.edu/~benh/260/Programming-in-Scala.pdf
谢谢大大的帮助了 !!!! O___Q