Setting package in Scala REPL

我是研究僧i 提交于 2019-11-30 05:05:31

问题


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:


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.



来源:https://stackoverflow.com/questions/25343217/setting-package-in-scala-repl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!