當我使用:在spark-shell中加載時,它看起來好像是單獨讀取行,因此伴隨對象不會在同一個「源」文件中讀取。 :粘貼似乎沒有參數。在spark-shell中加載伴隨對象
此前我正在用代碼構建和加載jar到spark-shell中,但希望將它作爲簡單腳本運行。有沒有人有最喜歡的解決方法?
當我使用:在spark-shell中加載時,它看起來好像是單獨讀取行,因此伴隨對象不會在同一個「源」文件中讀取。 :粘貼似乎沒有參數。在spark-shell中加載伴隨對象
此前我正在用代碼構建和加載jar到spark-shell中,但希望將它作爲簡單腳本運行。有沒有人有最喜歡的解決方法?
足夠新的shell將具有:粘貼文件。
或者,作爲一種解決方法,鏈接的模板,這種方式:加載它們:
class C(i: Int) {
def c = { println("C..."); i }
}; object C {
def apply(i: Int = 42) = new C(i)
}
或者,
scala> (new $intp.global.Run) compile List("C.scala")
scala> new C().c
C...
res1: Int = 42
更多API:
scala> import reflect.io._
import reflect.io._
scala> import reflect.internal.util._
import reflect.internal.util._
scala> val code = File("C.scala").slurp
code: String =
"
class C(i: Int) { def c = { println("C..."); i } }
object C { def apply(i: Int = 42) = new C(i) }
"
scala> $intp interpret code
defined class C
defined object C
res0: scala.tools.nsc.interpreter.IR.Result = Success
scala> C()
res1: C = [email protected]
同樣,
scala> $intp interpret s"object X { $code }"
defined object X
res0: scala.tools.nsc.interpreter.IR.Result = Success
scala> X.C()
res1: X.C = [email protected]
我的啓動腳本定義:
implicit class `interpreter interpolator`(val sc: StringContext) { def i(args: Any*) = $intp interpret sc.s(args: _*) }
爲
scala> i"val x = 42"
x: Int = 42
res0: scala.tools.nsc.interpreter.IR.Result = Success
這compile
招不會出現與 「腳本」 的文件工作。它期望源文件可由scalac
編譯,其中所有val
和def
聲明都在類型內。
因此,與:load
一起使用的另一種黑客方法是將案例類和伴隨對象寫入另一個對象。在這裏,我只是粘貼了代碼,不使用:paste
,但它也可以與:load
一起使用。
scala> object O {
| case class C(s: String)
| object C {
| def apply() = new C("<no string>")
| }
| }
defined module O
scala> O.C()
res0: O.C = C(<no string>)
腳本還允許repl命令;我的init.script將':paste file'與導入和其他定義混合在一起。 –