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.

1595 questions
18
votes
5 answers

How make implicit Ordered on java.time.LocalDate

I want to use java.time.LocalDate and java.time.LocalDateTime with an implicit Ordered like: val date1 = java.time.LocalDate.of(2000, 1, 1) val date2 = java.time.LocalDate.of(2010, 10, 10) if (date1 < date2) ... import…
Michael
  • 206
  • 1
  • 2
  • 6
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
  • 53,652
  • 11
  • 151
  • 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
  • 8,134
  • 6
  • 43
  • 70
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
  • 4,978
  • 20
  • 35
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,504
  • 19
  • 64
  • 109
17
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
  • 9,937
  • 10
  • 55
  • 105
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
  • 9,558
  • 9
  • 58
  • 79
16
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
  • 86,952
  • 56
  • 271
  • 360
16
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
  • 90,473
  • 43
  • 204
  • 301
16
votes
3 answers

popen implicitly declared even though #include is added

This is tiny snippet of my code. #include #include #include #include #include #include #include #include #include
Chris Allen
  • 541
  • 2
  • 8
  • 16
16
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,407
  • 4
  • 39
  • 61
16
votes
1 answer

Declare a Function `type` with `implicit` parameters

Is it somehow possible to declare something like type F = (Int, Boolean)(implicit String) => Unit in Scala?
scravy
  • 10,869
  • 11
  • 60
  • 115
16
votes
5 answers

Why Can A C# Class Inherit From One Interface Both Implicitly and Explicitly?

Today I happens to find that one C# class can inherit one interface both in implicit and explicit way. This surprises me. If C# works in this way, then one instance can behave differently when referenced in different way. interface IFoo { void…
Morgan Cheng
  • 66,562
  • 63
  • 166
  • 223
16
votes
2 answers

Partially applying a function that has an implicit parameter

Can I turn a method which takes an implicit parameter into a function? trait Tx def foo(bar: Any)(implicit tx: Tx) {} foo _ // error: could not find implicit value for parameter tx: Tx I am trying to achieve the following, preferably if I can…
0__
  • 64,257
  • 16
  • 158
  • 253
15
votes
2 answers

Confused about Scala method calling conventions, specifically the sum function on Seq

I was playing around with the new Scala IDE (Eclipse 3.6.2 + Scala IDE 2.0.0 [Scala 2.9.0]) and I tried to do something simple like this: (1 to 10).sum That works fine, but I've been doing a lot of Groovy also recently and I automatically wrote: (1…
Phuong LeCong
  • 1,814
  • 16
  • 19