-
Notifications
You must be signed in to change notification settings - Fork 29
/
FlatMap.scala
48 lines (43 loc) · 1.34 KB
/
FlatMap.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.thoughtworks.dsl
package keywords
import com.thoughtworks.dsl.Dsl
import Dsl.IsKeyword
import scala.util.NotGiven
final case class FlatMap[+Upstream, +Mapped](
upstream: Upstream,
flatMapper: _ => Mapped
) extends Dsl.Keyword.Trait
object FlatMap {
given [Upstream, UpstreamValue, Mapped, MappedValue](using
IsKeyword[Mapped, MappedValue]
): IsKeyword[FlatMap[Upstream, Mapped], MappedValue] with {}
given [
Upstream,
UpstreamValue,
Mapped,
MappedValue,
Domain
](using
upstreamDsl: Dsl.Searching[Upstream, Domain, UpstreamValue],
nestedDsl: Dsl.Searching[Mapped, Domain, MappedValue]
): Dsl.Composed[FlatMap[Upstream, Mapped], Domain, MappedValue] =
Dsl.Composed {
(
keyword: FlatMap[Upstream, Mapped],
handler: MappedValue => Domain
) =>
val FlatMap(upstream, flatMapper) = keyword
upstreamDsl(
upstream,
{ upstreamValue =>
// The typer might erase the type of of parameter of the function
// when the parameter is a reference to a local value, therefore,
// we are unable to call `flatMapper` without a cast.
nestedDsl(
flatMapper.asInstanceOf[UpstreamValue => Mapped](upstreamValue),
handler
)
}
)
}
}