You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/blog/_posts/2019-01-21-12th-dotty-milestone-release.md
+31-6Lines changed: 31 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -35,19 +35,44 @@ This is our 12th scheduled release according to our
35
35
### Extension Methods
36
36
37
37
We are excited to announce that extension methods are now offered through dedicated language support!
38
-
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.
39
-
Previously, one had to place the desired method in the trait with the receiver object to extend, as part of the parameter list.
40
-
The infix-ness of the method was being restored by manually writing the implicit class, resolving `implicitly` that extended object.
41
-
42
-
Now, in Dotty we can express an extension method by writing the method and prepending its name with the type to be extended.
38
+
Extension methods allow one to add methods to a type after the type is defined.
39
+
This is done by writing a method with a parameter for the type to be extended
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.
48
+
Extension methods are enabled when they are syntactically in scope (as above),
49
+
or when their enclosing instance is present in the implicit scope of the type that they extend,
50
+
as we exemplify below.
51
+
52
+
Extension methods were previously encoded in a rather roundabout way via the implicit class pattern.
53
+
Such encoding required a lot of boilerplate, especially when defining type classes.
54
+
In Dotty, this is no longer the case,
55
+
and type classes with infix syntax become very straightforward to define!
def (x: List[T]) combine (y: List[T]):List[T] = x ::: y
67
+
}
68
+
1.combine(2) // == 3
69
+
List(1,2).combine(List(3,4)) // == List(1,2,3,4)
70
+
```
71
+
72
+
This works because the `combine` extension methods of `IntSemigroup` and `ListSemigroup` are available
73
+
from the relevant implicit scopes.
74
+
75
+
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.
0 commit comments