Comment in the source for the Predef states:
The Predef object provides definitions that are accessible in all
Scala compilation units without explicit qualification.
How is Predefs inherited in Scala?
The compiler implicitly adds import _root_.scala.Predef._ to every source file.
Related
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.
Taken from "Scala with cats" (page 18):
Implicit Conversions
When you create a type class instance constructor using an implicit def, be sure to mark the parameters to the method as implicit parameters. Without this keyword, the compiler won’t be able to fill in the parameters during implicit resolution. implicit methods with non‐implicit parameters form a different Scala pattern called an implicit conversion. This is also different from the previous section on Interface Syntax, because in that case the JsonWriter is an implicit class with extension methods. Implicit conversion is an older programming pattern that is frowned upon in modern Scala code. Fortunately, the compiler will warn you when you do this. You have to manually enable implicit conversions by importing scala.language.implicitConversions in your file
Can anyone please sum up well why implicit conversion are deprecated? What were there limit or issues? Why the approach with implicit parameters is better?
Note I know how to use the modern approach well, including chaining implicit and all. I am just curious as to what was the issue and why it was deprecated.
Martin Odersky, the inventor of Scala, has indicated that implicits (and implicit conversions) are being deprecated in scala 3.1 and will eventually be removed from the language altogether.
the implicit functionality will be replaced with Extension Methods and Givens. Extension Methods and Givens provide a more tighter functional solution that doesn't introduce the unsuspecting and hidden side effects that implicits cause. Odersky now views implicits as a “recipe for disaster" and are "too implicit" which was his motivation to replace their functionality in 3.x.
https://www.slideshare.net/Lightbend/scala-3-is-coming-martin-odersky-shares-what-to-know
https://hub.packtpub.com/is-scala-3-0-a-new-language-all-together-martin-odersky-its-designer-says-yes-and-no/
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.
In the Scala reflection guide is written the following:
As with Manifests, one can in effect request that the compiler
generate a TypeTag. This is done by simply specifying an implicit
evidence parameter of type TypeTag[T]. If the compiler fails to find a
matching implicit value during implicit search, it will automatically
generate a TypeTag[T].
This StackOverflow answer beautifully explains the concept of "implicit evidence". However, it is still not completely clear to me what it means that the compiler will
generate a TypeTag[T].
Does this mean that this is a special case of "implicit evidence" search? I.e. the class TypeTag[T] is handled in a special way when the compiler does implicit search ? I tried to look for implicit parameter values in the Scala reflection APIs but I did not find any which provides a TypeTag[T], so I assume the TypeTag[T] implicit parameter is coming from inside the compiler (as the documentation says). So the classname TypeTag[T] is hardcoded into the compiler's source. Is this assumption correct ?
Is the automatic generation of implicit values documented somewhere? In other words, is there a documentation somewhere which lists all the automatically generated implicit evidences ? I did not find TypeTag[T] in the Scala language specification (version 2.9). The closest concept there to TypeTag[T] is Manifest which are automatically generated implicit parameters. Are Manifests the only automatically generated implicit value parameters in Scala 2.9 ?
Yes, TypeTags and WeakTypeTags are treated specially by implicit search. Now that implicit macros actually work, we plan to remove this hardcode, but that remains to be implemented.
So far there's no documentation for automatic generation of implicit values apart from source code, which says that only type tags and manifests are currently generated: https://github.com/scala/scala/blob/38ee986bcad30a8835e8f197112afb5cce2b76c5/src/compiler/scala/tools/nsc/typechecker/Implicits.scala#L1288
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.