Skip to content

Blog implicit functions v2 #571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions blog/_posts/2016-12-05-implicit-function-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ layout: blog
title: Implicit Function Types
author: Martin Odersky
authorImg: /images/martin.jpg
disqus: true
---

I just made the [first pull request](https://github.com/lampepfl/dotty/pull/1775) to add _implicit function types_ to
Expand Down Expand Up @@ -46,18 +47,18 @@ element and the site where it is used. Consequently, it becomes
tedious to define all those intermediate parameters and to pass them
along to where they are eventually consumed.

Implicit parameters solve one half of the problem. Implicit
parameters do not have to be propagated using boilerplate code; the
compiler takes care of that. This makes them practical in many
scenarios where plain parameters would be too cumbersome. For
instance, type classes would be a lot less popular if one would have
to pass all dictionaries by hand. Implicit parameters are also very
useful as a general context passing mechanism. For instance in the
_dotty_ compiler, almost every function takes an implicit context
parameter which defines all elements relating to the current state of
the compilation. This is in my experience much better than the cake
pattern because it is lightweight and can express context changes in a
purely functional way.
_Implicit parameters_ solve one half of the problem. They do not have to
be propagated using boilerplate code; the compiler takes care of
that. This makes them practical in many scenarios where plain
parameters would be too cumbersome. For instance, type classes would
be a lot less popular if one would have to pass all dictionaries by
hand. Implicit parameters are also very useful as a general context
passing mechanism. For instance in the _dotty_ compiler, almost every
function takes an implicit context parameter which defines all
elements relating to the current state of the compilation. This is in
my experience much better than the cake pattern because it is
lightweight and can express context changes in a purely functional
way.

The main downside of implicit parameters is the verbosity of their
declaration syntax. It's hard to illustrate this with a smallish example,
Expand Down Expand Up @@ -155,7 +156,7 @@ For instance, the _dotty_ compiler uses implicit abstraction
over contexts for most of its parts. Consequently it ends up with currently
no fewer than 2641 occurrences of the text string

(implicit ctx: Context)
(implicit ctx: Context)

It would be nice if we could get rid of them.

Expand Down Expand Up @@ -354,11 +355,15 @@ this blog post is already too long.

## Conclusion

Implicit function types are a unique way to abstract over the context in
which some piece of code is run. I believe they will deeply influence
the way we write Scala in the future. They are very powerful
Implicit function types are a unique way to abstract over the context
in which some piece of code is run. I believe they will deeply
influence the way we write Scala in the future. They are very powerful
abstractions, in the sense that just declaring a type of a function
will inject certain implicit values into the scope of the function's
implementation. Can this be abused, making code more obscure?
Absolutely, like every other powerful abstraction technique. To keep
your code sane, please keep the [Principle of Least Power](http://www.lihaoyi.com/post/StrategicScalaStylePrincipleofLeastPower.html) in mind.
implementation. Many use cases can profit from this power to
eliminate boilerplate and make code clearer. Can this be abused,
making code more obscure? Absolutely, like every other powerful
abstraction technique. To keep your code sane, please keep the
[Principle of Least
Power](http://www.lihaoyi.com/post/StrategicScalaStylePrincipleofLeastPower.html)
in mind.
Loading