2012-05-06 27 views
9

當我通過Cay S. Horstmann的「Scala for the Impatient」工作時,我注意到第一章中第一個練習揭示了一些有趣的內容。什麼是Scala REPL的標籤填寫告訴我這裏?

  1. 在Scala REPL中,鍵入3.然後是Tab鍵。可以應用哪些方法?

當我這樣做,我得到以下

 
scala> 3. 
%    &    *    +    -   /    
>    >=    >>    >>>   ^   asInstanceOf 
isInstanceOf toByte   toChar   toDouble  toFloat  toInt   
toLong   toShort  toString  unary_+  unary_-  unary_~   
|  

但我注意到,如果我按一下Tab鍵第二次,我得到一個稍微不同的列表。

 
scala> 3. 
!=    ##    %    &    *    +    
-   /       >=    >>    >>>   ^   asInstanceOf 
equals   getClass  hashCode  isInstanceOf toByte   toChar   
toDouble  toFloat  toInt   toLong   toShort  toString  
unary_+  unary_-  unary_~  |  

什麼是REPL試圖告訴我這裏?第二次出現的不同方法有什麼特別之處嗎?

回答

11

在REPL raises the verbosity of the completion擊中兩次Tab:

如果「方法名」是z的完井中,並verbosity > 0表明 標籤已經連續兩次按下,然後我們稱之爲alternativesFor 和顯示的列表重載的方法簽名。

interpreter source下面的方法說明什麼是過濾的方法完成時verbosity == 0(即,當你只打一次Tab並沒有得到alternativesFor版本):

def anyRefMethodsToShow = Set("isInstanceOf", "asInstanceOf", "toString") 

def excludeEndsWith: List[String] = Nil 

def excludeStartsWith: List[String] = List("<") // <byname>, <repeated>, etc. 

def excludeNames: List[String] = 
    (anyref.methodNames filterNot anyRefMethodsToShow) :+ "_root_" 

def exclude(name: String): Boolean = (
    (name contains "$") || 
    (excludeNames contains name) || 
    (excludeEndsWith exists (name endsWith _)) || 
    (excludeStartsWith exists (name startsWith _)) 
) 

因此,與一個標籤你得到的方法過濾了一些規則,解釋開發人員已經決定是合理和有用的。兩個選項卡爲您提供未經過濾的版本。

相關問題