Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading companion objects in spark-shell

When I use :load in the spark-shell it appears as though lines are read separately and thus companion objects are not read in the same "source" file. :paste does not appear to take arguments.

Previously I was building and loading a jar with my code into the spark-shell but was hoping to run it as a script for simplicity. Does anyone have a favorite workaround?

like image 653
Chris Avatar asked Jul 11 '26 19:07

Chris


1 Answers

A sufficiently recent shell will have :paste file.

Or, as a workaround, link the templates this way to :load them:

class C(i: Int) {
  def c = { println("C..."); i }
}; object C {
  def apply(i: Int = 42) = new C(i)
}

Or,

scala> (new $intp.global.Run) compile List("C.scala")

scala> new C().c
C...
res1: Int = 42

More 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 = C@f2f2cc1

Similarly,

scala> $intp interpret s"object X { $code }"
defined object X
res0: scala.tools.nsc.interpreter.IR.Result = Success

scala> X.C()
res1: X.C = X$C@7d322cad

My startup script defines:

implicit class `interpreter interpolator`(val sc: StringContext) { def i(args: Any*) = $intp interpret sc.s(args: _*) }

for

scala> i"val x = 42"
x: Int = 42
res0: scala.tools.nsc.interpreter.IR.Result = Success
like image 78
som-snytt Avatar answered Jul 14 '26 14:07

som-snytt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!