Scala ambiguous imports - scala

Scala beginner here and I was trying out the example here:
https://raw.githubusercontent.com/sryza/aas/master/ch02-intro/src/main/scala/com/cloudera/datascience/intro/RunIntro.scala
val nasRDD = parsed.map(md => {
md.scores.map(d => NAStatCounter(d))
})
The above gives me error:
<console>:51: error: reference to NAStatCounter is ambiguous;
it is imported twice in the same scope by
import $VAL180.NAStatCounter
and import INSTANCE.NAStatCounter
md.scores.map(d => NAStatCounter(d))
^
Can anyone please explain why this double import is happening. How can I avert this?

I could not reproduce your problem. I put RunIntro.scala into a small sbt project and compiled it successfully with the build.sbt file (with blank lines removed)
% cat build.sbt
name := "RunIntro"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies ++= List("org.apache.spark" % "spark-core_2.11" % "1.6.1")
The imports are only part of the source of your problem. How are you compiling this source?

Related

Why am I getting object scalatest is not a member of package org

I am trying to follow the Scala tutorial and am getting the error object scalatest is not a member of package org. All of the other instances of this error I can find here and on the web have the problem that the test file isn't under src/test, but that's not the case for me. I repeat, for the sake of those who keep marking this as a duplicate, the test file is under src/test, so that is not the problem. My build.sbt file says:
name := "TestExercise"
version := "0.1"
scalaVersion := "2.12.6"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % "test"
src/main/scala/CubeCalculator says:
object CubeCalculator extends App {
def cube(x: Int) = {
x * x * x
}
}
and src/test/scala/CubeCalculatorTest says:
import org.scalatest.FunSuite
class CubeCalculatorTest extends FunSuite {
test("CubeCalculator.cube") {
assert(CubeCalculator.cube(3) === 27)
}
}
(Cut-and-pasted straight from the tutorial.)
So what am I doing wrong? Why can't my project access scalatest?
#terminally-chill gave the answer. I am using Intellij, and File | Invalidate caches / restart, followed by a rebuild, resolved the issue.

error: not found: object play

Actually learning how to code with scala, I need some help on this:
import play.api.libs.json._
case class Alert(email: String, query: String)
{
def main(args: Array[String]): Unit = { println("Hello from main of class")}
}
I've got an error message :
Alert.scala:2: error: not found: object play
import play.api.libs.json._
One error found
I don't know where the problem come, I updated IntelliJ, and even added the missing library Dependencies in my build.sbt
name := """alert"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
jdbc,cache,ws,
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test,
"org.tpolecat" %% "doobie-core" % "0.4.1",)
I'm quite sure they explained it very well in this related question:
object play not found in scala application

compilation error on #js.native when added #js.native annotation to a trait on scalajs

It appears like #js.native is not identified by my compiler. In general scalajs compiles to me in the project.
link to file (and its including project in github where it fails).
source of the file that fails on #js.native
package example
import scala.scalajs.js
import js.annotation._
#js.native // sbt won't compile this native not found how to fix?
trait Funnel {
}
yields:
Funnel.scala:8: type native is not a member of package
scala.scalajs.js [error] #js.native [error] ^ [error] one error
found
sbt for reference:
import com.lihaoyi.workbench.Plugin._
enablePlugins(ScalaJSPlugin)
workbenchSettings
name := "Example"
version := "0.1-SNAPSHOT"
scalaVersion := "2.11.5"
libraryDependencies ++= Seq(
"org.scala-js" %%% "scalajs-dom" % "0.8.0",
"com.lihaoyi" %%% "scalatags" % "0.5.4"
)
jsDependencies += "org.webjars" % "d3js" % "3.5.12" / "d3.js"
jsDependencies += ProvidedJS / "d3-funnel.js"
bootSnippet := "example.ScalaJSExample().main(document.getElementById('canvas'));"
updateBrowsers <<= updateBrowsers.triggeredBy(fastOptJS in Compile)
Looks like you are running scala.js 0.6.1. Try to upgrade your version to >= 0.6.5

how to declare and use object in this build.sbt?

I am trying to understand how to write a well-written build.sbt file, and followed a tutorial on youtube. In that tutorial an object is created similar as below, but what I have written gives the shown error.
What am I doing wrong?
I have tried to remove blank lines between imports and the object declaration without any change.
import sbt._
import sbt.Keys._
object BuildScript extends Build {
lazy val commonSettings = Seq(
organization := "me",
version := "0.1.0",
scalaVersion := "2.11.4"
)
lazy val root = (project in file(".")).
settings(commonSettings: _*).
settings(
name := "deepLearning",
libraryDependencies += "org.deeplearning4j" % "deeplearning4j-core" % "0.4-rc3.4"
)}
error message:
error: illegal start of simple expression
object BuildScript extends Build {
^
[error] Error parsing expression. Ensure that there are no blank lines within a setting.
I think this thread actually explains it: What is the difference between build.sbt and build.scala?
I feel that the edit made by chris martin on this post was unnecessary, but can't reject it.
I think your tutorial was out of date. Using a recent version of sbt (0.13+ or so) you really want to do this:
lazy val commonSettings = Seq(
organization := "me",
version := "0.1.0",
scalaVersion := "2.11.4"
)
lazy val root = (project in file(".")).
settings(commonSettings).
settings(
name := "deepLearning",
libraryDependencies += "org.deeplearning4j" % "deeplearning4j-core" % "0.4-rc3.4"
)
If your project doesn't have any subprojects, though, the commonSettings val is somewhat superfluous, and you can just inline it:
lazy val root = (project in file(".")).
settings(
organization := "me",
name := "deepLearning",
version := "0.1.0",
scalaVersion := "2.11.4",
libraryDependencies += "org.deeplearning4j" % "deeplearning4j-core" % "0.4-rc3.4"
)
If you do have subprojects, and wind up with a lot of common settings, you may want to pull them out into an autoplugin, but that's a more advanced concept.
There are two ways to fix this:
Do object BuildScript extends Build { ... } and use .scala extension for the build file. I think this way is the recommended long term Sbt build file style.
Change build definition to gregsymons's answer and keep the .sbt extension.

I can not import filters in playframework 2.3.0

I use playframework 2.3.0, recently I want to add the CSRFFilter
when I import csrf in global.scala:
import play.filters.csrf._
I get an error for this:
[error] G:\testprojects\app\Global.scala:7: object filters is not a member
of package play
[error] import play.filters.csrf._
My plugin.sbt is
...
// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.0")
...
I use Build.scala instead of build.sbt
lazy val root = Project("root", base = file(".")).enablePlugins(PlayScala)
.settings(baseSettings: _*)
.settings(libraryDependencies++=appDependencies)
.settings(
scalaVersion := "2.11.1",
version := "1.0"
)
According to the documentation you have to add the filters dependency to your project:
libraryDependencies += filters
The documentation is for build.sbt but I guess it should work with Build.scala too.
Play Framework GzipFilter is working for me,
my build.sbt file
name := "GZIP"
version := "1.0-SNAPSHOT"
libraryDependencies ++= Seq(
javaJdbc,
javaEbean,
cache,
filters
)
play.Project.playJavaSettings
steps to get play.filters package
1. play
2. update //important
3. clean
4. eclipse
5. compile
6. run
finally it will work.... (update command is important)
if IDE not detecting play.filters
do the above steps one more time
finally copy paste below code
import play.GlobalSettings;
import play.api.mvc.EssentialFilter;
import play.filters.gzip.GzipFilter;
public class Global extends GlobalSettings {
public <T extends EssentialFilter> Class<T>[] filters() {
return new Class[]{GzipFilter.class};
}
}
In Play 2.4.3, the import is:
import play.filters.cors.CORSActionBuilder
It's no longer called csrf, but cors.