Questions tagged [implicit]

An implicit in Scala is a function applied or a parameter provided without explicitly appearing in the source code.

There are two variants of implicits in Scala: implicit conversions and implicit parameters.

Implicit conversions are applied by the scala compiler when it encounters an expression which it can not compile, but which can be compiled when one of the elements are passed to a function and the return value is used instead of the original element. A typical use case is the enrich-my-library pattern.

For example Scala allows the syntax "some.*regexp".r although the literal between the quotes is a String and String does not have a method r. But there is a method defined in Predef: augmentString(x: String): StringOps and StringOps has an r method. So this implicit conversion is applied.

Implicit parameters are parameters that are added to a method call by the compiler. A somewhat famous use case is in the collection library. Many methods accept a CanBuildFrom parameter which is used to define the type of collection returned by a method. In most cases this parameter is not specified explicitly but an implicit default value is used, which allows the collection library to return the "correct" specialized collection.

Only fields marked as such are considered for use as implicit parameters or conversions.

While this feature makes it possible to create powerful, concise and type safe DSLs and other APIs it should be used with care, since it also allows to create code that is rather hard to understand.

1780 questions
19
votes
4 answers

Why in C# an explicit decimal => long conversion operator is called implicitly, losing precision?

The following C# program silently and implicitly calls an explicit decimal-to-long conversion operator, losing precision. I don't understand why this happens. As far as I understand, in C# explicit operator should not be called implicitly by the…
kaalus
  • 4,475
  • 3
  • 29
  • 39
19
votes
1 answer

Not able to import Spark Implicits in ScalaTest

I am writing Test Cases for Spark using ScalaTest. import org.apache.spark.sql.SparkSession import org.scalatest.{BeforeAndAfterAll, FlatSpec} class ClassNameSpec extends FlatSpec with BeforeAndAfterAll { var spark: SparkSession = _ var…
himanshuIIITian
  • 5,985
  • 6
  • 50
  • 70
19
votes
1 answer

Magnet pattern and overloaded methods

There is a significant difference in how Scala resolves implicit conversions from "Magnet Pattern" for non-overloaded and overloaded methods. Suppose there is a trait Apply (a variation of a "Magnet Pattern") implemented as follows. trait Apply[A]…
Vladimir Kostyukov
  • 2,492
  • 3
  • 21
  • 30
19
votes
2 answers

Using context bounds "negatively" to ensure type class instance is absent from scope

tl;dr: How do I do something like the made up code below: def notFunctor[M[_] : Not[Functor]](m: M[_]) = s"$m is not a functor" The 'Not[Functor]', being the made up part here. I want it to succeed when the 'm' provided is not a Functor, and fail…
nadavwr
  • 1,820
  • 16
  • 20
19
votes
1 answer

Why do we still need a .lib stub file when we've got the actual .dll implementation?

i'm wondering why linkers can not do their job simply by consulting the information in the actual .dll files that got the actual implementation code ? i mean why linkers still need .lib files to do implicit linking ? are not the export and relative…
geeko
  • 2,649
  • 4
  • 32
  • 59
18
votes
2 answers

Passing scala.math.Integral as implicit parameter

I have read the answer to my question about scala.math.Integral but I do not understand what happens when Integral[T] is passed as an implicit parameter. (I think I understand the implicit parameters concept in general). Let's consider this…
Michael
  • 10,185
  • 12
  • 59
  • 110
18
votes
5 answers

Xcode C++ Vectors: Implicit instantiation of undefined template

I ran this code on a different IDE and it was successful. For some reason I get the above error message on Xcode. I assume I'm missing a header of some kind, but I'm not sure which one. #include #include #include…
Nathan Hale
  • 191
  • 1
  • 1
  • 3
18
votes
2 answers

Why can't the first parameter list of a class be implicit?

scala> class A(implicit a: Int); defined class A scala> class B()(implicit a: Int); defined class B scala> new A()(1) res1: A = A@159d450 scala> new B()(1) res2: B = B@171f735 scala> new A(1) :7: error: too many arguments for…
retronym
  • 54,768
  • 12
  • 155
  • 168
18
votes
3 answers

Why does this explicit call of a Scala method allow it to be implicitly resolved?

Why does this code fail to compile, but compiles successfully when I uncomment the indicated line? (I'm using Scala 2.8 nightly). It seems that explicitly calling string2Wrapper allows it to be used implicitly from that point on. class A { import…
Matt R
  • 9,892
  • 10
  • 50
  • 83
17
votes
1 answer

What does the keyword 'implicit' mean when it's placed in front of lambda expression parameter?

I have seen this kind of code many times before, most recently at scala-user mailing list: context(GUI) { implicit ec => // some code } context is defined as: def context[T](ec: ExecutionContext)(block: ExecutionContext => T): Unit = { ec…
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
17
votes
3 answers

Scala: Implicit parameter resolution precedence

Suppose we have implicit parameter lookup concerning only local scopes: trait CanFoo[A] { def foos(x: A): String } object Def { implicit object ImportIntFoo extends CanFoo[Int] { def foos(x: Int) = "ImportIntFoo:" + x.toString } } object…
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
17
votes
3 answers

How many implicits are there in Scala?

If I haven't imported anything but Scala's usual defaults, how many implicits (implicit conversions) are in scope? Is there a complete list of them somewhere, preferably organized by type that they could act upon?
kittylyst
  • 5,640
  • 2
  • 23
  • 36
17
votes
3 answers

Coding with Scala implicits in style

Are there any style guides that describe how to write code using Scala implicits? Implicits are really powerful, and therefore can be easily abused. Are there some general guidelines that say when implicits are appropriate and when using them…
dsg
  • 12,924
  • 21
  • 67
  • 111
17
votes
3 answers

Scala: reconciling type classes with dependency injection

There seems to be a lot of enthusiasm among Scala bloggers lately for the type classes pattern, in which a simple class has functionality added to it by an additional class conforming to some trait or pattern. As a vastly oversimplified example, the…
Marcus Downing
  • 10,054
  • 10
  • 63
  • 85
16
votes
4 answers

Need explanation difference between json and dict in Python

I am just curious to understand JSON and Dict in Python more deeply. I have a JSON response from a server like this: `{"city":"Mississauga","country":"Canada","countryCode":"CA"}` And I want to work with it as a dictionary. For this, I use .json()…
user1542557
  • 181
  • 1
  • 1
  • 4