I am getting this error in IntelliJ:
object breakOut is not a member of package collection
import scala.collection.{breakOut, mutable}
Can you suggest me which package I should add or if the can share the code for breakOut class maybe defining it will work ?
breakOut was in the Scala standard library until Scala 2.13. You shouldn't need to depend on any additional libraries. In Scala 2.13 and later, you should remove that parameter and explicitly convert to the required type, e. g. using toList, toMap, toSet or whatever it is that's required.
Related
I'm new in Kotlin and trying to convert an app written in Scala to Kotlin. In Scala there is the ListBuffer from scala.collection.mutable.ListBuffer. Is there an equivalent in Kotlin? I tried importing com.sun.tools.javac.util.ListBuffer but I would get an error when I try to compile: Kotlin: Symbol is declared in module 'jdk.compiler' which does not export package 'com.sun.tools.javac.util'?
In Kotlin you can use MutableList instead for the same purposes. Though I should note that Scala and Kotlin collections are very different in general, e.g. Kotlin prefers read-only collections to actually immutable ones and doesn't have a direct equivalent of Scala List. So if you are hoping to take a Scala library/app and change class names, it won't work unless your app is very trivial.
I'm still a bit confused about the scala Shapeless library after reading many articles. It seems that Shapeless uses scala compiling features? So does it use reflection and is it safe for production code?
Shapeless doesn't use reflection, it uses macros to inspect the structure of classes. With Shapeless this inspection happens at compilation time and not runtime (reflection happens at runtime). As a result of this Shapeless can be considered safer than reflection because it will be able to make many checks at compilation time.
Let's try to get a field by name using shapeless
case class MyClass(field: String)
import shapeless._
val myClassLens = lens[MyClass] >> 'field
val res = myClassLens.get(MyClass("value")) // res == "value"
if we use an invalid field name the compiler will complain with a compilation error
On the other hand if we tried to achieve this same thing using reflection the field name would be checked at runtime (maybe in production), that's why reflection is not considered as safe as Shapeless. It will also be way faster with Shapeless than reflection
Which implicits are present by default in Scala?
I know of RichString, Regex and some others I use.
But is there a list of them all?
Where are they implemented ? SourceFiles?
Is there a way to get a list of all possible implicit conversions for the current state of imports ?
The automatically imported members are:
The members of the java.lang package
The members of the scala package
The members of the Predef object
See The scala specification ยง9.1
Given that only objects (and package objects) can contain methods or values, the only place where standard (with no additional import) implicit values can be found is in Predef (the scala package does not seem to have a corresponding package object at the moment).
I believe all scala default imports are located in scala.Predef, including implicits.
Everything declared in Predef is imported automatically including implicits.
Check Predef at http://www.scala-lang.org/api/2.10.3/index.html#scala.Predef.
There you can see a list of value members. You should look for items starting with implicit def.
Simple question, I have a problem where using mapTo on the result of ask results in a compiler error along the lines of:
not found: value ClassTag
For example:
(job ? "Run").mapTo[Result]
^
I don't understand why it needs a ClassTag to do the cast? If I substitute a standard class from Predef like String as in (job ? "Run").mapTo[String] that compiles OK.
This happens when I define the class right above the line in question, as in:
class Result {}
(job ? "Run").mapTo[Result]
I still get the same problem.
Thanks, Jason.
I should also state that I'm using Scala 2.10.0 and Akka 2.1.0 (if that makes a difference).
This seems to be a particular problem with the Scala 2.10.0 version
After adding
import reflect.ClassTag
the implicitly used ClassTag parameter in mapTo should work.
Either that or updating to a newer Version of Akka/Scala (which should be prefered if possible).
What's the difference between something being defined in the scala package object and in Predef?
Is Predef just a relic from pre-2.8 when package objects didn't exist, or is there some other reason why we need both?
According to the ScalaDoc,
"The Predef object provides definitions that are accessible in all
Scala compilation units without explicit qualification"
So, it is not a package object itself, but acts as one in terms of providing functionality to "all Scala compilation units"
As for why the situation exists, I think you are right, looks to be a legacy issue
As for why it persists, there may continue to be limitations of package objects that prevent PreDef from being merged.