6
votes

Is there a way in the Scala REPL to set the "active" package scope ? Say I have a package com.package with class A, I want to be able to type new A() instead of new com.package.A() without explicitly doing import com.package.A. There might be a number of other classes in that package I'm interested into and I don't want to polute my REPL's global namespace by doing import com.package._.

Even better, I'd like to define class A without typing its fully qualified name. Something like:

package com.package // do this once

class A
class B

val a = new A()
val b = new B()

I'm aware of the :paste -raw command, but that would require me to type package com.package for each block; I'm really looking for a stateful command to change the "current working package", if you will.

1
So, I guess stackoverflow.com/a/18510229/6309 wouldn't be helpful either? - VonC
there is currently no support for that because packages are actually used in the internals of the REPL (classes defined in the REPL don't actually live in the root package), but that would make an interesting feature request... - gourlaysama
@VonC: No, this would not allow me to define new classes without a fully qualified name, as per my question. - gsimard
Ditto @gourlaysama The use case is testing existing classes with package-private access. - som-snytt

1 Answers

7
votes

Simply put, you cannot.

Each command in the scala REPL is wrapped into a newly generated package, as explained here.

Also, there was a ticket asking package { } support in the REPL, but it was dismissed as :paste -raw was considered enough for the purpose.