[問題] 關於scala的 symbol literals

看板Programming作者 (candyling)時間10年前 (2013/10/02 02:21), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/2 (看更多)
小妹最近剛開始接觸 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 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.240.182.141

10/04 00:16, , 1F
還有這種怪語法
10/04 00:16, 1F
文章代碼(AID): #1IInALn4 (Programming)
文章代碼(AID): #1IInALn4 (Programming)