Skip to content

Commit 034d033

Browse files
committed
Update reference docs
1 parent b4dd60b commit 034d033

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

docs/docs/reference/contextual/extension-methods.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,28 @@ extension [T: Numeric](x: T)
7474
def + (y: T): T = summon[Numeric[T]].plus(x, y)
7575
```
7676

77-
If an extension method has type parameters, they come immediately after `extension` and are followed by the extended parameter.
78-
When calling a generic extension method, any explicitly given type arguments follow the method name.
79-
So the `second` method could be instantiated as follows:
80-
77+
Type parameters on extensions can also be combined with type parameters on the methods
78+
themselves:
8179
```scala
82-
List(1, 2, 3).second[Int]
80+
extension [T](xs: List[T])
81+
def map [U](op: T => U): List[U] = ...
8382
```
8483

85-
Of course, the type argument here would usually be left out since it can be inferred.
86-
84+
Type arguments matching method type parameters are passed as usual:
85+
```scala
86+
List(1, 2, 3).map[String](_.toString)
87+
```
88+
By contrast, type arguments matching type parameters following `extension` can be passed
89+
only if the method is referenced as a regular method:
90+
```scala
91+
map[Int](List(1, 2, 3))(_ + 1)
92+
```
8793
Extensions can also take using clauses. For instance, the `+` extension above could equivalently be written with a using clause:
8894

8995
```scala
9096
extension [T](x: T)(using n: Numeric[T])
9197
def + (y: T): T = n.plus(x, y)
9298
```
93-
94-
**Note**: Type parameters have to be given after the `extension` keyword; they cannot be given after the `def`.
95-
This restriction might be lifted in the future once we support multiple type parameter clauses in a method.
96-
By contrast, using clauses can be defined for the `extension` as well as per `def`.
97-
9899
### Collective Extensions
99100

100101
Sometimes, one wants to define several extension methods that share the same

docs/docs/reference/contextual/type-classes.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ As in the previous example of Monoids, [`extension` methods](extension-methods.m
101101

102102
```scala
103103
trait Functor[F[_]]:
104-
extension [A, B](x: F[A])
105-
def map(f: A => B): F[B]
104+
extension [A](x: F[A])
105+
def map[B](f: A => B): F[B]
106106
```
107107

108108
The instance of `Functor` for `List` now becomes:
109109

110110
```scala
111111
given Functor[List] with
112-
extension [A, B](xs: List[A])
113-
def map(f: A => B): List[B] =
112+
extension [A](xs: List[A])
113+
def map[B](f: A => B): List[B] =
114114
xs.map(f) // List already has a `map` method
115115

116116
```
@@ -143,12 +143,12 @@ trait Monad[F[_]] extends Functor[F]:
143143
/** The unit value for a monad */
144144
def pure[A](x: A): F[A]
145145

146-
extension [A, B](x: F[A])
146+
extension [A](x: F[A])
147147
/** The fundamental composition operation */
148-
def flatMap(f: A => F[B]): F[B]
148+
def flatMap[B](f: A => F[B]): F[B]
149149

150150
/** The `map` operation can now be defined in terms of `flatMap` */
151-
def map(f: A => B) = x.flatMap(f.andThen(pure))
151+
def map[B](f: A => B) = x.flatMap(f.andThen(pure))
152152

153153
end Monad
154154
```
@@ -161,8 +161,8 @@ A `List` can be turned into a monad via this `given` instance:
161161
given listMonad: Monad[List] with
162162
def pure[A](x: A): List[A] =
163163
List(x)
164-
extension [A, B](xs: List[A])
165-
def flatMap(f: A => List[B]): List[B] =
164+
extension [A](xs: List[A])
165+
def flatMap[B](f: A => List[B]): List[B] =
166166
xs.flatMap(f) // rely on the existing `flatMap` method of `List`
167167
```
168168

@@ -178,8 +178,8 @@ it explicitly.
178178
given optionMonad: Monad[Option] with
179179
def pure[A](x: A): Option[A] =
180180
Option(x)
181-
extension [A, B](xo: Option[A])
182-
def flatMap(f: A => Option[B]): Option[B] = xo match
181+
extension [A](xo: Option[A])
182+
def flatMap[B](f: A => Option[B]): Option[B] = xo match
183183
case Some(x) => f(x)
184184
case None => None
185185
```
@@ -227,8 +227,8 @@ given configDependentMonad: Monad[ConfigDependent] with
227227
def pure[A](x: A): ConfigDependent[A] =
228228
config => x
229229

230-
extension [A, B](x: ConfigDependent[A])
231-
def flatMap(f: A => ConfigDependent[B]): ConfigDependent[B] =
230+
extension [A](x: ConfigDependent[A])
231+
def flatMap[B](f: A => ConfigDependent[B]): ConfigDependent[B] =
232232
config => f(x(config))(config)
233233

234234
end configDependentMonad
@@ -248,8 +248,8 @@ given configDependentMonad: Monad[[Result] =>> Config => Result] with
248248
def pure[A](x: A): Config => A =
249249
config => x
250250

251-
extension [A, B](x: Config => A)
252-
def flatMap(f: A => Config => B): Config => B =
251+
extension [A](x: Config => A)
252+
def flatMap[B](f: A => Config => B): Config => B =
253253
config => f(x(config))(config)
254254

255255
end configDependentMonad
@@ -263,8 +263,8 @@ given readerMonad[Ctx]: Monad[[X] =>> Ctx => X] with
263263
def pure[A](x: A): Ctx => A =
264264
ctx => x
265265

266-
extension [A, B](x: Ctx => A)
267-
def flatMap(f: A => Ctx => B): Ctx => B =
266+
extension [A](x: Ctx => A)
267+
def flatMap[B](f: A => Ctx => B): Ctx => B =
268268
ctx => f(x(ctx))(ctx)
269269

270270
end readerMonad

0 commit comments

Comments
 (0)