Can't import scala.reflect.runtime.universe - scala

I'd like to play around with reflection in scala (2.10.2) by following the example in
this tutorial. things work fine when I start the sbt (version 0.13) and import
scala.refelct.runtime.universe._
scala> import scala.reflect.runtime.universe._ │~
import scala.reflect.runtime.universe._
but when I try to put the sample code to an object like
object ReflectExample {
import scala.reflect.runtime.universe._
/*
the rest of the Example
*/
}
and compile the code by sbt compile I see the following error message like:
[error] object runtime is not a member of package reflect
[error] import scala.reflect.runtime.universe._

As explained in sbt's documentation, you need to add this line to the libraryDependencies field of your project in build.sbt:
"org.scala-lang" % "scala-reflect" % scalaVersion.value

You may want to try adding dependency to http://mvnrepository.com/artifact/org.scala-lang/scala-reflect

Related

package cats contains object and package with same name: implicits

Heard about the new Cats-Effect library here
http://typelevel.org/blog/2017/05/02/io-monad-for-cats.html
Immidiately added the following line to my ammonite shell predef.sc
interp.load.ivy("org.typelevel" % "cats-core_2.12" % "0.9.0")
interp.load.ivy( "org.typelevel" % "cats-effect_2.12" % "0.1-0848c9b")
Now when I load my ammonite shell. I get error
cmd0.sc:1: package cats contains object and package with same name: implicits
one of them needs to be removed from classpath
import cats.effect.IO
^
Compilation Failed
Googled and found a solution here
Package contains object and package with same name
But I wonder how can I apply the -Yresolve-term-conflict:strategy to the ammonite shell?
I was able to solve it myself. Here are the right imports
interp.load.ivy("org.typelevel" %% "cats" % "0.9.0")
interp.load.ivy( "org.typelevel" % "cats-effect_2.12" % "0.1-0848c9b")
Now everything works fine
# import cats._
import cats._
# import cats.effect.IO
import cats.effect.IO
#

How to find the missing Akka Dependencies?

I write a Scala project managed by SBT and I want to use TestKit package (http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0/scala/stream-testkit.html#TestKit).
In order to do it, I have to do the following imports:
import system.dispatcher
import akka.pattern.pipe
The problem is that I have to update build.sbt file in order to "bring" the needed dependencies. I tried to add the following dependency:
"com.typesafe.akka" %% "akka-http-testkit" % 2.4.2
and I still got an error for the lines:
import system.dispatcher
and
val probe = TestProbe()
Can you give me a hint on how to find the needed dependency given an import line?
Try to add:
"com.typesafe.akka" %% "akka-http-testkit" % 2.4.2 % Test
akka-http-teskit is a test library, so you should be adding it to the Test scope.

How to write unit tests for Spark Streaming programs?

I'm relatively new to using Spark Streaming. I've been searching for the best way to write unit tests for my Spark application and came across the TestSuiteBase trait.
However, I'm unable to extend this trait in my test suite.
Here's the code snippet releant to this issue:
...
import org.apache.spark.rdd.RDD
import org.apache.spark.streaming._
import org.apache.spark.streaming.TestSuiteBase
...
...
class UnitTest extends BaseTest with TestSuiteBase
...
However, I hit this error when running sbt test:
.... object TestSuiteBase is not a member of package org.apache.spark.streaming
[error] import org.apache.spark.streaming.TestSuiteBase
Also, are there any better approaches to writing unit tests for Spark Streaming programs?
Any help would be appreciated.
Thanks in advance.
I modified my "build.sbt" to contain:
libraryDependencies += "org.apache.spark" %% "spark-streaming" % "2.4.0" classifier "tests"
This will include the test jars for spark streaming, which contains TestSuiteBase

Repl showing error: object async is not a member of package scala on import

I am using scala 2.11.7 Repl (ubuntu) when trying to do this import I am getting :
scala> import scala.async.Async._
<console>:18: error: object async is not a member of package scala
import scala.async.Async._
^
how can I fix that ?
scala-async is not part of Scala standard library. You need to import the module in your project.
If you use sbt, you can add the following to your build file.
libraryDependencies += "org.scala-lang.modules" %% "scala-async" %
"0.9.5"
Check out this PR about the lack of information about this.
Hope this helps!
Use the following command to load the jar into classpath,
scala -classpath jarname.jar

SBT can't resolve dependency in build definition

I'm making an SBT task that needs to make a multipart POST request to a certain server. I want to use Dispatch to make the request. I have the following in build.sbt at the top level of my project:
libraryDependencies ++= Seq(
"net.databinder.dispatch" %% "dispatch-core" % "0.9.5"
)
The task definition is in project/Build.scala. I have
import sbt._
import Keys._
import dispatch._
object SubmitBuild extends Build {
...
}
I get the following error message:
[error] /Users/ken/xxxxtools/project/Build.scala:3: not found: object dispatch
[error] import dispatch._
[error] ^
If I remove import dispatch._ then sbt will compile. I know I have Dispatch installed. Why can't SBT find it?
If you want to make references in Build.scala to some dependency it has to be declared in build's project not in the "project project". Meaning that it should be project/build.sbt.
It turns out that project/Build.scala is also a SBT project in the same way your project is.
SBT authors give a very good explanation in sbt is recursive.