-
Notifications
You must be signed in to change notification settings - Fork 29
/
Match.scala
63 lines (58 loc) · 1.78 KB
/
Match.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.thoughtworks.dsl
package keywords
import Dsl.IsKeyword
type Match[+Keyword, +CaseSet] = keywords.FlatMap[Keyword, CaseSet]
object Match {
export FlatMap.apply
case class WithIndex[Index <: Int, +Keyword](index: Index, keyword: Keyword)
extends Dsl.Keyword.Trait
opaque type +:[+A, +B] = A | B
object WithIndex:
given [
Index <: Int,
Keyword,
Domain,
LastValue
](using
dsl: Dsl.Searching[Keyword, Domain, LastValue],
valueOfIndex: ValueOf[Index]
): Dsl.Composed[WithIndex[Index, Keyword] +: Nothing, Domain, LastValue] =
Dsl.Composed {
(
keywordWithIndex: WithIndex[Index, Keyword] +: Nothing,
handler: LastValue => Domain
) =>
keywordWithIndex match {
case WithIndex(valueOfIndex.value, keyword) =>
dsl(keyword, handler)
case _ =>
throw new IllegalArgumentException("Invalid index")
}
}
given [
Index <: Int,
LeftKeyword,
RestKeyword,
Domain,
Value
](using
leftDsl: Dsl.Searching[LeftKeyword, Domain, Value],
valueOfIndex: ValueOf[Index],
restDsl: Dsl.Searching[RestKeyword, Domain, Value]
): Dsl.Composed[WithIndex[
Index,
LeftKeyword
] +: RestKeyword, Domain, Value] = Dsl.Composed {
(
keywordUnion: WithIndex[Index, LeftKeyword] +: RestKeyword,
handler: Value => Domain
) =>
keywordUnion match {
case WithIndex(valueOfIndex.value, leftKeyword) =>
leftDsl(leftKeyword.asInstanceOf[LeftKeyword], handler)
case _ =>
restDsl(keywordUnion.asInstanceOf[RestKeyword], handler)
}
}
}
// @inline def Match = keywords.FlatMap