将包含选项卡的代码粘贴到scala repl中

时间:2016-07-05 22:32:10

标签: scala read-eval-print-loop

我正在使用

:paste

在复制显示包含制表符的scala代码段之前。但即使这样,也会发生以下情况:

scala> :paste
// Entering paste mode (ctrl-D to finish)

type Moves = Seq[Board]
val EmptyMoves = Seq[Board]()

def allMoves() = {
Display all 413 possibilities? (y or n)
e = (‘x’,’o’).map{ case xo =>
Display all 413 possibilities? (y or n)
dex
Display all 413 possibilities? (y or n)
t, ix % 3) }

那么是否有任何方式能够将带有标签的代码放入repl中?

2 个答案:

答案 0 :(得分:2)

你需要这个issue #1975到REPL。

或者,您可以实施修复并提供-Dscala.repl.reader=my.Reader

另一个答案建议找到劫持读者的方法:

scala> :power
Power mode enabled. :phase is at typer.
import scala.tools.nsc._, intp.global._, definitions._
Try :help or completions for vals._ and power._

scala> import scala.tools.nsc.interpreter._, java.io._
import scala.tools.nsc.interpreter._
import java.io._

scala> def f(code: String) = repl.savingReader {
     | repl.in = new SimpleReader(new BufferedReader(new StringReader(code)), new PrintWriter(scala.Console.out), false)
     | repl.loop() }
f: (code: String)$r.repl.LineResults.LineResult

scala> f("val x = 42. toInt")    // embedded tab
x: Int = 42
res5: $r.repl.LineResults.LineResult = EOF

或前面的编辑​​:

$ scala -Xnojline
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions for evaluation. Or try :help.

scala> 

scala> val x = 42.  toInt
x: Int = 42

或完成前述:

$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions for evaluation. Or try :help.

scala> val x = 42.             // 42 tab toInt
!=   /    >=          ceil          getClass        isPosInfinity   isWhole     shortValue       toDegrees     toOctalString   underlying   
%    <    >>          compare       intValue        isValidByte     longValue   signum           toDouble      toRadians       until        
&    <<   >>>         compareTo     isInfinite      isValidChar     max         to               toFloat       toShort         |            
*    <=   ^           doubleValue   isInfinity      isValidInt      min         toBinaryString   toHexString   unary_+                      
+    ==   abs         floatValue    isNaN           isValidLong     round       toByte           toInt         unary_-                      
-    >    byteValue   floor         isNegInfinity   isValidShort    self        toChar           toLong        unary_~                      

scala> val x = 42.toInt
x: Int = 42

scala> :power
Power mode enabled. :phase is at typer.
import scala.tools.nsc._, intp.global._, definitions._
Try :help or completions for vals._ and power._

scala> import scala.tools.nsc.interpreter._
import scala.tools.nsc.interpreter._

scala> repl.in = new jline.InteractiveReader(() => NoCompletion)
repl.in: scala.tools.nsc.interpreter.InteractiveReader = scala.tools.nsc.interpreter.jline.InteractiveReader@10660795

scala> val x = 42.toInt
x: Int = 42

答案 1 :(得分:1)

您可以将代码粘贴到文件中,然后在REPL中执行以下命令:

 scala> :load YourFile.scala

NB :文件不应包含声明,否则加载将失败。