在Javascript中我可以描述像這樣何時Scala的功能執行
function showString(){ console.log("this is a string") };
這樣的功能,在控制檯有功能和執行功能之間的嚴格區別
> function showString(){ console.log("this is a string") };
> showString
function showString(){ console.log("this is a string") }
> showString()
this is a string
在Scala中我現在正在做同樣的事情;
def showname() = println("this is a string")
然而,當我運行這是在控制檯,它似乎總是要執行的函數,而不是還能夠只是繞過功能:
scala> def showname() = println("this is a string")
showname:()Unit
scala> showname // I am expecting a function, not an executed function
this is a string
scala> showname()
this is a string // I am expecting an executed function
是Scala的處理功能不同?我的期望錯了嗎?
我認爲*函數對象*是一個更好的名字,因爲你在_應用之後得到的東西(雖然,不理想,因爲scala中的對象帶有另一個含義 - 單例)。 – 2014-08-29 13:51:38
@ om-nom-nom我明白你的觀點,但我覺得說實話很混亂,我也從來沒有讀過這種命名方式(或者我沒有注意到),但我始終堅持使用它。 – 2014-08-29 14:17:26
好的:-)我只提出了替代方案。 *我從來沒有讀過這種命名*,[它比較流行](https://www.google.com/search?q=「function + object」+ scala&oq =「function + object」+ scala)。 – 2014-08-29 14:29:42