首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >粘贴模式/ kotlin REPL中的多行段?

粘贴模式/ kotlin REPL中的多行段?
EN

Stack Overflow用户
提问于 2021-01-17 22:04:17
回答 2查看 268关注 0票数 0

给定一个函数的骨架:

代码语言:javascript
复制
fun reformat(
    str: String,
    normalizeCase: Boolean = true,
    upperCaseFirstLetter: Boolean = true,
    divideByCamelHumps: Boolean = false,
    wordSeparator: Char = ' ',
) {
  println("In reformat")
}

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

代码语言:javascript
复制
>>> 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中的:paste

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-18 15:33:43

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

代码语言:javascript
复制
[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

特别是通知:

代码语言:javascript
复制
[1] :p
// Entering paste mode (ctrl-D to finish)

这正是我要找的。它确实有效(尽管它使用了丑陋的JDK9警告):

代码语言:javascript
复制
[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
票数 0
EN

Stack Overflow用户

发布于 2021-01-17 22:37:36

它看起来像Kotlin REPL打算从IDE中使用。如果您从Intellij IDEA内部打开它,它就像一种魅力,允许您进行多行输入。我看不出有什么选项可以让你在普通的shell中做同样的事情。因此,在您的示例中,您可以在一行中声明函数,也可以从文件中加载脚本。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65766461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档