[問題] 關於scala的 symbol literals

作者: lingcandy (candyling)   2013-10-02 02:21:06
小妹最近剛開始接觸 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

Links booklink

Contact Us: admin [ a t ] ucptt.com