Intellij Scala math functions import - scala

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.

Related

can not resolve symbol after import the correct package in intellij

I want to use the when() method in org.apache.spark.sql.Column, when I go ahead, it turns out to be like this.
enter image description here
I have tried things like
IntelliJ inspection gives "Cannot resolve symbol" but still compiles code
but it just doesn't work, what should I do now?
when() is inside the org.apache.spark.sql.functions
So you need to import the functions as
import org.apache.spark.sql.functions._
or
import org.apache.spark.sql.functions.when

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.

Scala-IDE or Scala strange import behavior

I am working on a small Scala project. I have the following issue with 'import':
If, at the top of one of my files, I import two thing with these commands:
import main.Main._
import main.game.Game
^^^^
it gives me the following error message at the underlined 'main' word: "missing arguments for method main in object Main; follow this method with `_' if you want to treat it as a partially applied function" which is quite strange especially that it is just an import statement. And naturally no actual importing occures. At first I thought about semicolon inference quirks again but it is not the case. If I swap the two lines and write like this:
import main.game.Game
import main.Main._
then everythinng is fine.
Could anyone shed some light on that? Is it something special about Scala?
Presumably you have a main method in object Main. So after import main.Main._ main refers to this method instead of the main package. You could avoid it in several ways:
Change import order, as in the question.
Don't import the main method, as Daniel C. Sobral's answer suggests.
Explicitly say you want the top-level main package:
import _root_.main.game.Game
Following the normal Java package naming convention should avoid this problem in most cases, as you are unlikely to have members (or subpackages) called com or org (though net could be a problem).
You do have a method named main inside main.Main, don't you? Well, since you imported it, it has now shadowed the package by the name main. You can try this to confirm:
import main.Main.{main => _, _}
import main.game.Game
This will exclude main from being imported.

Scala's Relative Package Imports

I have a multi-project Scala workspace in eclipse. I think I'm getting hosed by my lack of understanding of the way Scala imports packages, but after spending more time than I care to admit looking for a solution, I can't figure this one out. I have recreated the problem in a simple 2 project setup.
Project 1: com.foo.mathematics contains a simple Vector class
Contains one file:
package com.foo.mathematics
class Vector2D(x : Double, y : Double) {
def length = math.sqrt(x*x + y*y)
}
Project 2: com.foo.analysis
package com.foo.analysis
import com.foo.mathematics.Vector2D
class Frame(xAxis : Vector2D, yAxis : Vector2D) {
}
Eclipse shows an error in the import line, The error message that I get is: Object mathematics is not a member of the package com.foo.
In the outline view, my import statement says this:
com.foo.analysis.<error: <none>>.Vector2D
I have tried changing the import to:
import mathematics.Vector2D
import _root_.com.foo.mathematics.Vector2D
neither one works...
What am I missing?
Both import com.foo.mathmatics.Vector2D and import _root_.com.foo.mathmatics.Vector2D should be fine. Most likely you either haven't added the first project to the build path of the second (see Build Path > Configure Build Path in the context menu), or need to clean the second project (Project > Build Clean) after making changes in the first project.
(Also, mathmatics looks like a typo for mathematics, so double check that you really have the same name in both places.)
Relative package imports don't come into it, they just mean you could write it this way:
package com.foo
package analysis
import mathmatics.Vector2D
class Frame(xAxis : Vector2D, yAxis : Vector2D) {
}

eclipse does not treat a scalatest flatspec as junit test

here is my test case , while i right click the file eclipse doest not show any run as junit test option. I try to manual create run configuration but does not take any sense.
scala version:2.8.1 scalatest:1.3 eclipse:3.6.2
package org.jilen.cache.segment
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
#RunWith(classOf[JUnitRunner])
class RandomSegmentSpec extends FlatSpec with ShouldMatchers {
val option = SegmentOptions()
"A Simple Segment" should "contains (douglas,lea) after put into" in {
val segment = RandomSegment.newSegment(option)
segment.put("douglas", "lea")
segment("douglas") should be("lea")
}
it should "return null after (douglas,lea) is remove" in {
val segment = RandomSegment.newSegment(option)
segment.put("douglas", "lea")
segment -= ("douglas")
segment("douglas") should equal(null)
}
it should "contains nothing after clear" in {
val segment = RandomSegment.newSegment(option)
segment.put("jilen", "zhang")
segment.put(10, "ten")
segment += ("douglas" -> "lea")
segment += ("20" -> 20)
segment.clear()
segment.isEmpty should be(true)
}
}
I've encountered this seemingly randomly, and I think I've finally figured out why.
Unfortunately the plugin doesn't yet change package declarations when you move files, nor the class names when you rename files. (Given you can put multiple classes in one file, the latter will likely never be done.) If you are used to the renamings being done automagically in Eclipse, like I am, you're bound to get caught on this.
So... check carefully the following:
the package declaration in your Scala file matches the Eclipse package name
the name of the test class in the Scala file matches the name of the Scala file
I just ran into this, fixed both, and now my test runs!
This is a known problem with the Eclipse IDE for Scala. I'm currently working on the plugin for this. Watch this space.
I found Scalatest to be very bad at integrating with Eclipse (running the tests from eclipse showed that it ran them - but they would not pass or fail, but simply show up as passive blank boxes).
For some reason I could NOT get it to work after 3 hours of trying things!
Finally I tried specs2 - and it worked (Scala 2.9, Junit4 and Eclipse 3.6)!
They have a great doc here:
http://etorreborre.github.com/specs2/guide/org.specs2.guide.Runners.html#Runners+guide
Since I don't care which testing framework to use, I will try Specs2 purely from the convenience point of view.