When trying to import breeze.plot (to use its function Feature()) from Scala REPL, this way:
import breeze.plot._
I get the following error:
object *plot* is not a member of package *breeze*.
As a novice user of Scala, I have not understood this answer.
Could you please tell me IN PLAIN WORDS what I have to do from Scala REPL?
Related
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
I'm looking at example Spark code and I'm a bit confused as to why the sample code I'm looking at requires two import statements:
import org.apache.spark._
import org.apache.spark.SparkContext._
This is Scala. As I understand it, _ is the wildcard character. So this looks like I'm importing SparkContext twice. Can anybody shed light on this?
This first line says to import all of the classes in the package org.apache.spark. This means you can use all of those classes without prefixing them with the package name.
The second line says to import all of the static members of the class SparkContext. This means you can use those members without prefixing their names with the class name.
Remember import doesn't really do anything at run time; it just lets you write less code. You aren't actually "importing" anything twice. The use of the term import comes from Java, and admittedly it is confusing.
This might help:
Without the first line, you would have to say
org.apache.spark.SparkContext
but the first import line lets you say
SparkContext
If you had only the first line and not the second, you would have to write
SparkContext.getOrCreate
but with both import lines you can just write
getOrCreate
I have a basic question about scalacheck's whenever clause. For some reason, my compiler doesn't recognize whenever, nor the (conditional subset) ==> part.
(I am following along Odersky's second scala course on Coursera, and I've written a scalacheck property as:
property("deleteMin ...") = forAll{
h:H => whenever (isEmpty(h)) {...
The compiler doesn't recognize whenever. Is there something I need to import additionally to
import org.scalacheck._
import Arbitrary._
import Gen._
import Prop._
?
I'm not an expert on scalacheck, but I have completed the Coursera assignment.
It can be done without whenever.
I can't find whenever mentioned in the API documentation.
Scalacheck doesn't have the "whenever" function, but you can use the ==> method instead. (you will need to import org.scalacheck.Prop.BooleanOperators)
If you want to use scalatest property based testing instead of scalacheck
you can mix in the Trait PropertyChecks (import org.scalatest.prop.PropertyChecks) and you can use the "whenever" function.
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.
how can I import the following package:
org.hibernate.type.StringType
in Scala? If I do:
import org.hibernate.type.StringType
"type" is recognized as a keyword. This is the second time I have run into this in two days. My last solution was the change my (Java) package name. This is no longer a valid solution!
Here is the message from Scala IDE:
<error> is not a member of org{org.type}.hibernate{org.hibernate.type}
Wrap the keyword with backquotes:
import org.hibernate.`type`.StringType
This trick also works when calling methods, which names are keywords in Scala.