@@ -8,10 +8,9 @@ partof: FAQ
8
8
num : 7
9
9
---
10
10
11
- An _ implicit_ question to newcomers to Scala seems to be: where does the
12
- compiler look for implicits? I mean implicit because the question never seems
13
- to get fully formed, as if there weren't words for it. :-) For example, where
14
- do the values for ` integral ` below come from?
11
+ Newcomers to Scala often ask: Where does the compiler look for implicits?
12
+
13
+ For example, where do the values for ` integral ` below come from?
15
14
16
15
scala> import scala.math._
17
16
import scala.math._
@@ -27,17 +26,17 @@ do the values for `integral` below come from?
27
26
scala> foo(0L)
28
27
scala.math.Numeric$LongIsIntegral$@48c610af
29
28
30
- Another question that does follow up to those who decide to learn the answer to
31
- the first question is how does the compiler choose which implicit to use, in
32
- certain situations of apparent ambiguity (but that compile anyway)?
29
+ The natural continuation of this line of inquiry leads to a second question: How
30
+ does the compiler choose which implicit to use, in certain situations of apparent
31
+ ambiguity (but that compile anyway)?
33
32
34
33
For instance, ` scala.Predef ` defines two conversions from ` String ` : one to
35
34
` WrappedString ` and another to ` StringOps ` . Both classes, however, share a lot
36
35
of methods, so why doesn't Scala complain about ambiguity when, say, calling
37
36
` map ` ?
38
37
39
38
** Note:** this question was inspired by [ this other question on Stack
40
- Overflow] [ 4 ] , but stating the problem in more general terms. The example was
39
+ Overflow] [ 4 ] , but states the problem in more general terms. The example was
41
40
copied from there, because it is referred to in the answer.
42
41
43
42
## Types of Implicits
@@ -84,10 +83,10 @@ implicit parameter. For example:
84
83
getIndex("abc", 'a')
85
84
86
85
The method ` getIndex ` can receive any object, as long as there is an implicit
87
- conversion available from its class to ` Seq[T] ` . Because of that, I can pass a
88
- ` String ` to ` getIndex ` , and it will work.
86
+ conversion available from its class to ` Seq[T] ` . Because of that, a ` String ` can be
87
+ passed to ` getIndex ` , and it will work.
89
88
90
- Behind the scenes, the compile changes ` seq.IndexOf(value) ` to
89
+ Behind the scenes, the compiler changes ` seq.IndexOf(value) ` to
91
90
` conv(seq).indexOf(value) ` .
92
91
93
92
### Context Bounds
@@ -97,9 +96,9 @@ pattern enables the provision of common interfaces to classes which did not
97
96
declare them. It can both serve as a bridge pattern -- gaining separation of
98
97
concerns -- and as an adapter pattern.
99
98
100
- The ` Integral ` class you mentioned is a classic example of type class pattern.
101
- Another example on Scala's standard library is ` Ordering ` . There's a library
102
- that makes heavy use of this pattern, called Scalaz .
99
+ The ` Integral ` class mentioned above is a classic example of type class pattern.
100
+ Another example on Scala's standard library is ` Ordering ` . Scalaz is a library
101
+ that makes heavy use of this pattern.
103
102
104
103
This is an example of its use:
105
104
@@ -134,16 +133,16 @@ a method which does not exist on the object's class, or because you are calling
134
133
a method that requires an implicit parameter, it will search for an implicit
135
134
that will fit the need.
136
135
137
- This search obey certain rules that define which implicits are visible and
136
+ This search obeys certain rules that define which implicits are visible and
138
137
which are not. The following table showing where the compiler will search for
139
138
implicits was taken from an excellent [ presentation] [ 1 ] about implicits by Josh
140
- Suereth, which I heartily recommend to anyone wanting to improve their Scala
139
+ Suereth, which is heartily recommend to anyone wanting to improve their Scala
141
140
knowledge. It has been complemented since then with feedback and updates.
142
141
143
- The implicits available under number 1 below has precedence over the ones under
142
+ The implicits available under number 1 below have precedence over the ones under
144
143
number 2. Other than that, if there are several eligible arguments which match
145
144
the implicit parameter’s type, a most specific one will be chosen using the rules
146
- of static overloading resolution (see [ Scala Specification] [ 5 ] §6.26.3 ).
145
+ of static overloading resolution (see [ Scala Specification] [ 5 ] §6.26.4 ).
147
146
148
147
1 . First look in current scope
149
148
* Implicits defined in current scope
@@ -155,7 +154,6 @@ of static overloading resolution (see [Scala Specification][5] §6.26.3).
155
154
* Implicit scope of an argument's type ** (2.9.1)**
156
155
* Implicit scope of type arguments ** (2.8.0)**
157
156
* Outer objects for nested types
158
- * Other dimensions
159
157
160
158
Let's give examples for them.
161
159
@@ -202,7 +200,7 @@ example:
202
200
y <- Some('x')
203
201
} yield (x, y)
204
202
205
- That expression is translated by the compile into
203
+ That expression is translated by the compiler into
206
204
207
205
List(1, 2, 3).flatMap(x => Some('x').map(y => (x, y)))
208
206
@@ -228,18 +226,18 @@ Note that companion objects of super classes are also looked into. For example:
228
226
val b = new B(5, 2)
229
227
val s: String = b // s == "A: 2"
230
228
231
- This is how Scala found the implicit ` Numeric[Int] ` and ` Numeric[Long] ` in your
232
- question , by the way, as they are found inside ` Numeric ` , not ` Integral ` .
229
+ This is how Scala found the implicit ` Numeric[Int] ` and ` Numeric[Long] ` in the
230
+ opening example , by the way, as they are found inside ` Numeric ` , not ` Integral ` .
233
231
234
232
### Implicit scope of an argument's type
235
233
236
234
If you have a method with an argument type ` A ` , then the implicit scope of type
237
- ` A ` will also be considered. By "implicit scope" I mean that all these rules
235
+ ` A ` will also be considered. Here "implicit scope" means all these rules
238
236
will be applied recursively -- for example, the companion object of ` A ` will be
239
237
searched for implicits, as per the rule above.
240
238
241
239
Note that this does not mean the implicit scope of ` A ` will be searched for
242
- conversions of that parameter, but of the whole expression. For example:
240
+ conversions of that parameter alone , but of the whole expression. For example:
243
241
244
242
class A(val n: Int) {
245
243
def +(other: A) = new A(n + other.n)
@@ -285,17 +283,15 @@ implicits are found inside companion objects to the type parameters of
285
283
` CanBuildFrom ` .
286
284
287
285
** Note** : ` Ordering ` is defined as ` trait Ordering[T] ` , where ` T ` is a type
288
- parameter. Previously, I said that Scala looked inside type parameters, which
289
- doesn't make much sense. The implicit looked for above is ` Ordering[A] ` , where
286
+ parameter. The implicit looked for above is ` Ordering[A] ` , where
290
287
` A ` is an actual type, not type parameter: it is a _ type argument_ to
291
- ` Ordering ` . See section 7.2 of the [ Scala Specification] [ 5 ] .
288
+ ` Ordering ` . See section 7.2 of the [ Scala Specification] [ 6 ] .
292
289
293
290
** This available only since Scala 2.8.0.**
294
291
295
292
### Outer Objects for Nested Types
296
293
297
- I haven't actually seen examples of this. I'd be grateful if someone could
298
- share one. The principle is simple:
294
+ The principle is simple:
299
295
300
296
class A(val n: Int) {
301
297
class B(val m: Int) { require(m < n) }
@@ -307,14 +303,13 @@ share one. The principle is simple:
307
303
val b = new a.B(3)
308
304
val s: String = b // s == "B: 3"
309
305
310
- ### Other Dimensions
306
+ A real world example of this would be welcome. Please share your example!
311
307
312
- I'm pretty sure this was a joke, but this answer might not be up-to-date. So
313
- don't take this question as being the final arbiter of what is happening, and
314
- if you do noticed it has gotten out-of-date, do open a ticket about it, or, if
315
- you know how to correct it, please fix it. It's a wiki after all.
308
+ ### Call To Action
316
309
317
- ** EDIT**
310
+ Avoid taking this question as being the final arbiter of what is happening.
311
+ If you do notice it has become out-of-date, do [ open a ticket about it] [ 7 ] , or, if
312
+ you know how to correct it, please fix it.
318
313
319
314
Related questions of interest:
320
315
@@ -327,5 +322,7 @@ This question and answer were originally submitted on [Stack Overflow][3].
327
322
[ 2 ] : https://issues.scala-lang.org/browse/SI-4427
328
323
[ 3 ] : http://stackoverflow.com/q/5598085/53013
329
324
[ 4 ] : http://stackoverflow.com/questions/5512397/passing-scala-math-integral-as-implicit-parameter
330
- [ 5 ] : http://scala-lang.org/files/archive/spec/2.11/
325
+ [ 5 ] : http://scala-lang.org/files/archive/spec/2.11/06-expressions.html
326
+ [ 6 ] : http://scala-lang.org/files/archive/spec/2.11/07-implicits.html
327
+ [ 7 ] : https://github.com/scala/scala.github.com/issues
331
328
0 commit comments