【问题标题】:Paste mode / multi-line-snippet in the kotlin REPL?kotlin REPL 中的粘贴模式/多行代码段?
【发布时间】:2021-01-17 22:04:17
【问题描述】:

给定一个函数的骨架:

fun reformat(
    str: String,
    normalizeCase: Boolean = true,
    upperCaseFirstLetter: Boolean = true,
    divideByCamelHumps: Boolean = false,
    wordSeparator: Char = ' ',
) {
  println("In reformat")
}

我将它粘贴到 kotlin REPL 中,它抱怨 - 显然是因为没有意识到它被发送了一个多行 sn-p:

>>> fun reformat(
error: parameter name expected
fun reformat(
             ^
error: expecting comma or ')'
fun reformat(
             ^
error: expecting ')'
fun reformat(
             ^

>>>     str: String,
error: expecting an element
    str: String,
       ^
error: expecting an element
    str: String,
               ^

etc ..

scala REPL 中的:paste 的等价物是什么?

【问题讨论】:

    标签: kotlin paste read-eval-print-loop


    【解决方案1】:

    看起来 Kotlin REPL 旨在从 IDE 中使用。如果您从 Intellij IDEA 中打开它,它就像一个魅力,并允许您进行多行输入。我没有看到任何允许您从常规 shell 执行相同操作的选项。因此,在您的情况下,您可以在一行中声明一个函数,也可以从文件中加载脚本。

    【讨论】:

    • 哇,真是太糟糕了。我是 JetBrains 的狂热爱好者(自 2008 年以来),但有时我只是不想要打开 IDE 的开销,即使是 python(vs Pycharm)或 scala(vs IJ)
    【解决方案2】:

    我找到了一个项目 - 显然由 JetBrains kotlin 开发人员自己积极改进,它解决了这个问题和其他问题:https://github.com/Kotlin/kotlin-interactive-shell。这是他们的帮助

    [0] :h
    :quit or q                           quit the shell
    :load or l <path>                    load file and evaluate
    :type or t <expr>                    display the type of an expression without evaluating it
    :list or ls                          list defined symbols
    :help or h [command]                 print this summary or command-specific help
    :paste or p                          enter paste mode
    :syntax {on | off}                   syntax highlighting
    :prompt [pattern]                    customize prompt
    :set                                 set configuration parameter
    :conf                                list configuration parameters
    :dependsOn <artifact coordinates>    Load dependency from the current set of repositories
    :repository <repository coordinates> Add repository for the artifacts lookup
    :classpath or cp                     Show current script compilation classpath
    
    

    特别注意:

    [1] :p
    // Entering paste mode (ctrl-D to finish)
    
    

    这正是我正在寻找的。它确实有效(尽管带有丑陋的 JDK9 警告):

    [1] :p
    // Entering paste mode (ctrl-D to finish)
    fun reformat(
        str: String,
        normalizeCase: Boolean = true,
        upperCaseFirstLetter: Boolean = true,
        divideByCamelHumps: Boolean = false,
        wordSeparator: Char = ' ',
    ) {
      println("In reformat")
    }
    
    // Exiting paste mode, now interpreting.
    WARNING: An illegal reflective access operation has occurred
    WARNING: Illegal reflective access by org.jetbrains.kotlin.com.intellij.util.ReflectionUtil (file:/Users/steve/git/kotlin-interactive-shell/ki-shell/target/ki-shell-0.4-SNAPSHOT-shaded.jar) to method java.util.ResourceBundle.setParent(java.util.ResourceBundle)
    WARNING: Please consider reporting this to the maintainers of org.jetbrains.kotlin.com.intellij.util.ReflectionUtil
    WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
    WARNING: All illegal access operations will be denied in a future release
    [2] reformat()
    ERROR No value passed for parameter 'str' (Line_3.kts:1:10)
    [3] reformat("abc")
    In reformat
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      • 2021-05-16
      • 2017-11-22
      相关资源
      最近更新 更多