|
2 | 2 | layout: blog
|
3 | 3 | post-type: blog
|
4 | 4 | title: Implicit Function Types
|
5 |
| -by: Martin Odersky |
| 5 | +author: Martin Odersky |
| 6 | +authorImg: /images/martin.jpg |
6 | 7 | ---
|
7 | 8 |
|
8 | 9 | I just made the [first pull request](https://github.com/lampepfl/dotty/pull/1775) to add _implicit function types_ to
|
@@ -46,18 +47,18 @@ element and the site where it is used. Consequently, it becomes
|
46 | 47 | tedious to define all those intermediate parameters and to pass them
|
47 | 48 | along to where they are eventually consumed.
|
48 | 49 |
|
49 |
| -Implicit parameters solve one half of the problem. Implicit |
50 |
| -parameters do not have to be propagated using boilerplate code; the |
51 |
| -compiler takes care of that. This makes them practical in many |
52 |
| -scenarios where plain parameters would be too cumbersome. For |
53 |
| -instance, type classes would be a lot less popular if one would have |
54 |
| -to pass all dictionaries by hand. Implicit parameters are also very |
55 |
| -useful as a general context passing mechanism. For instance in the |
56 |
| -_dotty_ compiler, almost every function takes an implicit context |
57 |
| -parameter which defines all elements relating to the current state of |
58 |
| -the compilation. This is in my experience much better than the cake |
59 |
| -pattern because it is lightweight and can express context changes in a |
60 |
| -purely functional way. |
| 50 | +_Implicit parameters_ solve one half of the problem. They do not have to |
| 51 | +be propagated using boilerplate code; the compiler takes care of |
| 52 | +that. This makes them practical in many scenarios where plain |
| 53 | +parameters would be too cumbersome. For instance, type classes would |
| 54 | +be a lot less popular if one would have to pass all dictionaries by |
| 55 | +hand. Implicit parameters are also very useful as a general context |
| 56 | +passing mechanism. For instance in the _dotty_ compiler, almost every |
| 57 | +function takes an implicit context parameter which defines all |
| 58 | +elements relating to the current state of the compilation. This is in |
| 59 | +my experience much better than the cake pattern because it is |
| 60 | +lightweight and can express context changes in a purely functional |
| 61 | +way. |
61 | 62 |
|
62 | 63 | The main downside of implicit parameters is the verbosity of their
|
63 | 64 | declaration syntax. It's hard to illustrate this with a smallish example,
|
@@ -354,11 +355,15 @@ this blog post is already too long.
|
354 | 355 |
|
355 | 356 | ## Conclusion
|
356 | 357 |
|
357 |
| -Implicit function types are a unique way to abstract over the context in |
358 |
| -which some piece of code is run. I believe they will deeply influence |
359 |
| -the way we write Scala in the future. They are very powerful |
| 358 | +Implicit function types are a unique way to abstract over the context |
| 359 | +in which some piece of code is run. I believe they will deeply |
| 360 | +influence the way we write Scala in the future. They are very powerful |
360 | 361 | abstractions, in the sense that just declaring a type of a function
|
361 | 362 | will inject certain implicit values into the scope of the function's
|
362 |
| -implementation. Can this be abused, making code more obscure? |
363 |
| -Absolutely, like every other powerful abstraction technique. To keep |
364 |
| -your code sane, please keep the [Principle of Least Power](http://www.lihaoyi.com/post/StrategicScalaStylePrincipleofLeastPower.html) in mind. |
| 363 | +implementation. A lot of use cases can profit from this power to |
| 364 | +eliminate boilerplate and make code clearer. Can this be abused, |
| 365 | +making code more obscure? Absolutely, like every other powerful |
| 366 | +abstraction technique. To keep your code sane, please keep the |
| 367 | +[Principle of Least |
| 368 | +Power](http://www.lihaoyi.com/post/StrategicScalaStylePrincipleofLeastPower.html) |
| 369 | +in mind. |
0 commit comments