2012-05-06 28 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 _)) 
) 

因此,与一个标签你得到的方法过滤了一些规则,解释开发人员已经决定是合理和有用的。两个选项卡为您提供未经过滤的版本。

相关问题