Scala - Check if a class is imported or not - scala

In Scala console the command:
import testPackage._
will give the below output:
scala> import testPackage._
import testPackage._
But after importing how to check what are the classes imported in the console or how to list the classes (of testPackage) in the console (just for verification). Please help.

Within the REPL I'm not sure if there's a command for listing all imported classes, however what you can do is use the tabbed completion, just type in:
scala> val tmp : testPackage.
and then hit TAB. You should get a list of the types available within that scope.
HTH

Related

importing a library that contains a same "namespaces" as Scala Option

I'm going to use Args4j java library to parse command line arguments, but while importing the library: import org.kohsuke.args4j.{CmdLineException, CmdLineParser, Option} it steps over the scala Option.
As I use Option in the same file, I end up having issues as the scala Option is being recognized as an Args4j Option.
I solved it by importing the Args4j lib inside the Object I actually going to use it, but I was wondering if there was a better way to solve this allowing me to have all imports grouped on top.
Thanks!
You can "rename" type during import:
import org.kohsuke.args4j.{CmdLineException, CmdLineParser, Option => ArgOption}
to avoid clashes.
Object[A]
ArgObject
Other thing would be importing package instead of its elements:
import org.kohsuke.args4j
Option[A]
arg4j.Option
or use full name for Scala's option
scala.Option[A]
Option

Intellij Scala math functions import

New to Intellij IDEA/Scala so I'm wondering is there shortcut to auto import Scala packages.
Example:
package test
object TestClass extends App{
var i = pow(22,22)
println("Hello World" + i );
}
It wont compile until import statment is added
import scala.math._
Coming from Eclipse/Java I expected CRTL + Shift O (or auto import) would offered me math package, must I type import myself ?
Sometimes yes, sometimes no. It depends on what you're searching for.
If you write math IntelliJ doesn't know what that is. If you write Math., that's already in scope and it will offer a menu of methods on the Math object.
If you write Date, alt-enter should bring up a menu of import options. Choose one and the import statement will be inserted into your code.
No, not necessarily.
In your settings in IntelliJ you can set up auto import by following these instructions. Alternatively, when you try and use a package that you do not have imported, it will tell you that it does not recognize what you are doing and show a red error. You can then autofill from the error (typically hit alt+enter) and it should solve the issue.

IntelliJ IDEA warns about Scala imports instead of showing type info when importing object members

I have code similar to below
import sqlCtx.implicits._
val items = sc.parallelize(List(i1, i2, i3))
items.toDF().registerTempTable("items")
When I hover over items I would like usual behaviour - displaying type information. Instead I get warnings Avoid wildcard imports and Imports should be grouped together. I can get rid of the first by importing specific function, like
import sqlCtx.implicits.rddToDataFrameHolder
but I can't put import on top of the file what IntelliJ expects of me since it imports from an object that is created with the code and not preexisting. How to workaround it?
I use IntelliJ IDEA v. 15.0.3 with the latest Scala plugin.

Fixing REPL completion candidates for wildcard imports

There is currently a severe REPL limitation:
scala> import concurrent._
import concurrent._
scala> Fut<tab>
This doesn't complete to Future. In other words, wildcard imports are not understood by the JLineCompletion.
I am trying to work around this. I am able to find the ImportHandler instances which report importsWildcard == true, but they are otherwise empty except for the plain importString (e.g., "import concurrent._").
How do I get a list of these wildcard imports, so I can fix the completion candidates?
One idea is as follows: I can get the completions for concurrent.<tab>, so somehow there must be already the functionality to look up the members of a package via the IMain instance.
This problem doesn't affect current versions of Scala. I tried the sbt console with Scala 2.12.4, and tab completion works with wildcard imports. (Beware of this problem, through).

How to reload a class or package in Scala REPL?

I almost always have a Scala REPL session or two open, which makes it very easy to give Java or Scala classes a quick test. But if I change a class and recompile it, the REPL continues with the old one loaded. Is there a way to get it to reload the class, rather than having to restart the REPL?
Just to give a concrete example, suppose we have the file Test.scala:
object Test { def hello = "Hello World" }
We compile it and start the REPL:
~/pkg/scala-2.8.0.Beta1-prerelease$ bin/scala
Welcome to Scala version 2.8.0.Beta1-prerelease
(Java HotSpot(TM) Server VM, Java 1.6.0_16).
Type in expressions to have them evaluated.
Type :help for more information.
scala> Test.hello
res0: java.lang.String = Hello World
Then we change the source file to
object Test {
def hello = "Hello World"
def goodbye = "Goodbye, Cruel World"
}
but we can't use it:
scala> Test.goodbye
<console>:5: error: value goodbye is not a member of object Test
Test.goodbye
^
scala> import Test;
<console>:1: error: '.' expected but ';' found.
import Test;
There is an alternative to reloading the class if the goal is to not have to repeat previous commands. The REPL has the command
:replay
which restarts the REPL environment and plays back all previous valid commands. (The invalid ones are skipped, so if it was wrong before, it won't suddenly work.) When the REPL is reset, it does reload classes, so new commands can use the contents of recompiled classes (in fact, the old commands will also use those recompiled classes).
This is not a general solution, but is a useful shortcut to extend an individual session with re-computable state.
Note: this applies to the bare Scala REPL. If you run it from SBT or some other environment, it may or may not work depending on how SBT or the other environment packages up classes--if you don't update what is on the actual classpath being used, of course it won't work!
Class reloading is not an easy problem. In fact, it's something that the JVM makes very difficult. You do have a couple options though:
Start the Scala REPL in debug mode. The JVM debugger has some built-in reloading which works on the method level. It won't help you with the case you gave, but it would handle something simple like changing a method implementation.
Use JRebel (http://www.zeroturnaround.com/jrebel). JRebel is basically a super-charged class reloading solution for the
JVM. It can handle
member addition/removal, new/removed classes, definition changes, etc. Just about the only thing it can't handle is changes in class hierarchy (adding a super-interface, for
example). It's not a free tool, but they do offer a complementary license which is limited to Scala compilation units.
Unfortunately, both of these are limited by the Scala REPL's implementation details. I use JRebel, and it usually does the trick, but there are still cases where the REPL will not reflect the reloaded class(es). Still, it's better than nothing.
There is an command meet you requirement
:load path/to/file.scala
which will reload the scala source file and recompiled to classes , then you can replay you code
This works for me....
If your new source file Test.scala looks something like this...
package com.tests
object Test {
def hello = "Hello World"
def goodbye = "Goodbye, Cruel World"
}
You first have to load the new changes into Scala console (REPL).
:load src/main/scala/com/tests/examples/Test.scala
Then re-import the package so you can reference the new code in Scala console.
import com.tests.Test
Now enjoy your new code without restarting your session :)
scala> Test.goodbye
res0: String = Goodbye, Cruel World
If the .scala file is in the directory where you start the REPL you can ommit the full path, just put :load myfile.scala, and then import.