【问题标题】:What is Scala REPL's tab completion telling me here?Scala REPL 的选项卡补全在这里告诉我什么?
【发布时间】:2012-05-06 17:33:47
【问题描述】:

在学习 Cay S. Horstmann 的“不耐烦的 Scala”时,我注意到第一章的第一个练习揭示了一些有趣的东西。

  1. 在 Scala REPL 中,键入 3. 后跟 Tab 键。可以应用哪些方法?

当我这样做时,我得到以下信息

斯卡拉> 3。 % & * + - / > >= >> >>> ^ asInstanceOf isInstanceOf toByte toChar toDouble toFloat toInt toLong toShort toString unary_+ unary_- unary_~ |

但我注意到,如果我第二次点击 Tab,我会得到一个稍微不同的列表。

斯卡拉> 3。 != ## % & * + - / >= >> >>> ^ asInstanceOf 等于 getClass hashCode isInstanceOf toByte toChar toDouble toFloat toInt toLong toShort toString 一元_+ 一元_- 一元_~ |

REPL 想在这里告诉我什么?第二次出现的不同方法有什么特别之处吗?

【问题讨论】:

    标签: scala read-eval-print-loop tab-completion scala-2.9


    【解决方案1】:

    在 REPL raises the verbosity of the completion 中点击两次标签:

    如果“methodName”在z的完成中,并且verbosity > 0表示 tab 已经连续按了两次,然后我们调用alternativesFor 并显示重载方法签名列表。

    interpreter source 中的以下方法表示在 verbosity == 0 时(即,当您只点击一次标签并且没有获得 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 _))
    )
    

    因此,通过一个选项卡,您将获得由解释器开发人员认为合理且有用的一些规则过滤的方法。两个选项卡为您提供未经过滤的版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-11
      • 1970-01-01
      • 2015-12-19
      • 2023-03-12
      • 2017-04-06
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      相关资源
      最近更新 更多