# Implicit Function Types

**URL:** <https://contributors.scala-lang.org/t/implicit-function-types/219>\
**Category:** Dotty\
**Created:** [December 7, 2016, 8:08pm UTC](https://contributors.scala-lang.org/t/implicit-function-types/219 "2016-12-07T20:08:56Z")\
**Posts on this page:** 1\
**Showing post:** 39

<div class="post-metadata">

**Author:** ![pweisenburger](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/pweisenburger/32/401_2.png) [@pweisenburger](https://contributors.scala-lang.org/u/pweisenburger)\
**Post date:** [May 22, 2018, 9:54am UTC](https://contributors.scala-lang.org/t/implicit-function-types/219/39 "2018-05-22T09:54:21Z")

</div>

> [@AMatveev](#):
>
> It will be great to replace anonymous classes with implicit Function Types. However, if we do this, methods “where” and “for update” will be global.

> [@](#):
>
> How can we add “context dependency” in such case?
> 
> ```scala
> for( ra <- OQuery(EmployeeAta.Type) { implicit q: OQuery =>
> import OQuery.helper._
> // ...
> }
> 
> ```

Ideas for this kind of tighter scoping of contextual identifiers were brought up quite some time ago by @lihaoyi and @stanch on the [scala-debate mailing list](http://groups.google.com/forum/#!topic/scala-debate/f4CLmYShX6Q). Short recap: They used [scala-async](http://github.com/scala/scala-async) as an example, which provides two main operations: `async` and `await`. `await` is defined to be only meaningful within the context of an `async` block. However, as scala-async behaves today, `await` is simply sitting in the global scope cluttering the namespace:

```scala
import scala.async.Async.{async, await}

val future = async {
  val f1 = async { ...; true }
  val f2 = async { ...; 42 }
  if (await(f1)) await(f2) else 0
}

```

With _Scope Injection_, you could define `async` as:

```scala
object Holder {
  val await = ???
}
def async[T](thunk: import Holder => T): Future[T] = ???

```

Which would give you a call-site syntax:

```scala
import scala.async.Async.async

val future = async {
  val f1 = async { ...; true }
  val f2 = async { ...; 42 }
  if (await(f1)) await(f2) else 0
}

```

There were also some other ideas for boilerplate-free implicit and scope injection, especially useful for writing DSLs in Scala (among them one idea which is basically Dotty’s _implicit function types_). You can find more in the current [README](http://github.com/pweisenburger/dslparadise/blob/master/README.md).

# DSL Paradise: Current State

The initial discussion resulted in an early prototype: [http://github.com/dsl-paradise/dsl-paradise](http://github.com/dsl-paradise/dsl-paradise)

I took the liberty to continue the work on this prototype. The current state is a Scala compiler plugin that supports the proposed implicit context propagation (i.e., implicit functions for Scala 2) and scope injection: [GitHub - pweisenburger/dslparadise: Scala compiler plugin for boilerplate-free context propagation and scope injection · GitHub](http://github.com/pweisenburger/dslparadise)

It supports the following three use cases:

- **Implicit Context Propagation**

- **Scope Injection**

- **Static Scope Injection**

The current implementation defines the implicit and import functions simply as type aliases for the standard function type (and thus, retains compatibility with standard Scala):

```scala
type `implicit =>`[-T, +R] = T => R
type `implicit import =>`[-T, +R] = T => R
type `import =>`[-T, +R] = T => R
type `import`[T, I] = T

```

**Nested Contexts:** When nesting implicit functions, using a fresh name for each compiler-generated implicit argument can result in ambiguous implicit values (this problem is solved in Dotty by refining the precedence rules for implicit resolution).

This compiler plugin allows to specify a fixed name to be used for the implicit argument to enable implicit argument resolution for nested contexts by shadowing the implicit argument of the outer context (the [README](http://github.com/pweisenburger/dslparadise/blob/master/README.md) gives a more [detailed example](http://github.com/pweisenburger/dslparadise/blob/master/README.md#nested-contexts)).

---

_[View the full topic](https://contributors.scala-lang.org/t/implicit-function-types/219)._
