Skip to content

Clarify blog post about extension methods #5771

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

Merged
merged 1 commit into from
Jan 22, 2019
Merged
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
37 changes: 31 additions & 6 deletions docs/blog/_posts/2019-01-21-12th-dotty-milestone-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,44 @@ This is our 12th scheduled release according to our
### Extension Methods

We are excited to announce that extension methods are now offered through dedicated language support!
Extension methods allow one to add methods to a type after the type is defined and up until now they were encoded through the powerful mechanism of implicits.
Previously, one had to place the desired method in the trait with the receiver object to extend, as part of the parameter list.
The infix-ness of the method was being restored by manually writing the implicit class, resolving `implicitly` that extended object.

Now, in Dotty we can express an extension method by writing the method and prepending its name with the type to be extended.
Extension methods allow one to add methods to a type after the type is defined.
This is done by writing a method with a parameter for the type to be extended
_on the left-hand side_ of the method name:

```scala
case class Circle(x: Double, y: Double, radius: Double)

def (c: Circle) circumference: Double = c.radius * math.Pi * 2
```

Read the [relevant documentation](https://dotty.epfl.ch/docs/reference/other-new-features/extension-methods.html) about generic extension methods, higher-kinded extension methods and more.
Extension methods are enabled when they are syntactically in scope (as above),
or when their enclosing instance is present in the implicit scope of the type that they extend,
as we exemplify below.

Extension methods were previously encoded in a rather roundabout way via the implicit class pattern.
Such encoding required a lot of boilerplate, especially when defining type classes.
In Dotty, this is no longer the case,
and type classes with infix syntax become very straightforward to define!
For example, consider:

```scala
trait Semigroup[T] {
def (x: T) combine (y: T): T
}
implicit val IntSemigroup: Semigroup[Int] = new {
def (x: Int) combine (y: Int): Int = x + y
}
implicit def ListSemigroup[T]: Semigroup[List[T]] = new {
def (x: List[T]) combine (y: List[T]): List[T] = x ::: y
}
1.combine(2) // == 3
List(1,2).combine(List(3,4)) // == List(1,2,3,4)
```

This works because the `combine` extension methods of `IntSemigroup` and `ListSemigroup` are available
from the relevant implicit scopes.

Read the [full documentation](https://dotty.epfl.ch/docs/reference/other-new-features/extension-methods.html) about generic extension methods, higher-kinded extension methods, and more.

### TASTy Reflect goodies

Expand Down