How can I make scala's ammonite use scala.util instead of ammonite.util as default for util?

419 views Asked by At

In the "official" scala REPL I can do

scala> import util.Random
scala> util.Random.nextInt
res0: Int => -306696783

but in Ammonite-REPL I get

@ import util.Random
cmd3.sc:1: object Random is not a member of pack ammonite.util
import util.Random
       ^
Compilation Failed

So right now I have to use the scala. prefix to make it work in Ammonite:

@ import scala.util.Random
@ scala.util.Random.nextInt
res1: Int = 503117434

I'm kind of new to Scala so I don't get why would ammonite use a different util than the (for me) "official" util, so I would appreciate if anyone can provide a rationale for this.

And more specifically, Is there any way to make util to be scala.util instead of ammonite.util?

1

There are 1 answers

0
Aron T On BEST ANSWER

It's not that Ammonite is replacing a different util library for the normal Scala one, it's that the Ammonite namespace has it's own util package which has a whole bunch of methods specific to Ammonite. Perhaps it would be nicer if the developer had chosen a different name for his package, but this is not an issue specific to Ammonite. It is something you will run into all the time. When there is a clash of namespaces, your only option is to fully qualify the package name for the one you want. So what you actually did is a fine solution. You can find more about this here.

And btw, since there is no util.Random in the Ammonite package you can do this after your import--I tested and this is a cut and paste from my terminal:

@ Random.nextInt res1: Int = 1045964363

When you do actually have a collision of method names, you can find a solution here