-
Notifications
You must be signed in to change notification settings - Fork 437
/
Copy.kt
79 lines (72 loc) · 1.79 KB
/
Copy.kt
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package arrow.optics
import kotlin.experimental.ExperimentalTypeInference
@DslMarker
public annotation class OpticsCopyMarker
@OpticsCopyMarker
public interface Copy<A> {
/**
* Changes the value of the element(s) pointed by the [Setter].
*/
public infix fun <B> Setter<A, B>.set(b: B)
/**
* Transforms the value of the element(s) pointed by the [Traversal].
*/
public infix fun <B> Traversal<A, B>.transform(f: (B) -> B)
/**
* Declares a block in which all optics are nested within
* the given [field]. Instead of:
*
* ```
* x.copy {
* X.a.this set "A"
* X.a.that set "B"
* }
* ```
*
* you can write:
*
* ```
* x.copy {
* inside(X.a) {
* A.this set "A"
* A.that set "B"
* }
* }
* ```
*/
@OptIn(ExperimentalTypeInference::class)
public fun <B> inside(field: Traversal<A, B>, @BuilderInference f: Copy<B>.() -> Unit): Unit =
field.transform { it.copy(f) }
}
// mutable builder of copies
private class CopyImpl<A>(var current: A): Copy<A> {
override fun <B> Setter<A, B>.set(b: B) {
current = this.set(current, b)
}
override fun <B> Traversal<A, B>.transform(f: (B) -> B) {
current = this.modify(current, f)
}
}
/**
* Small DSL which parallel Kotlin's built-in `copy`,
* but using optics instead of field names. See [Copy]
* for the operations allowed inside the block.
*
* This allows declaring changes on nested elements,
* preventing the "nested `copy` problem". Instead of:
*
* ```
* person.copy(address = person.address.copy(city = "Madrid"))
* ```
*
* you can write:
*
* ```
* person.copy {
* Person.address.city set "Madrid"
* }
* ```
*/
@OptIn(ExperimentalTypeInference::class)
public fun <A> A.copy(@BuilderInference f: Copy<A>.() -> Unit): A =
CopyImpl(this).also(f).current