-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathImplementation.kt
58 lines (43 loc) · 1.11 KB
/
Implementation.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
package co.zsmb.villagedsl.advanced.dsl2
import co.zsmb.villagedsl.advanced.models.*
@DslMarker
annotation class AdvancedDSL2
class HouseBuilder {
internal val people = mutableListOf<Person>()
internal val items = mutableListOf<Item>()
fun build(): House {
return House(people, items)
}
@AdvancedDSL2
infix fun String.age(age: Int) {
people.add(Person(this, age))
}
@AdvancedDSL2
val Int.gold: Unit
get() {
items += Gold(this)
return Unit
}
}
class VillageBuilder {
private val houses = mutableListOf<House>()
@AdvancedDSL2
operator fun House.unaryPlus() {
houses += this
}
@AdvancedDSL2
fun house(setup: HouseBuilder.() -> Unit = {}) {
val houseBuilder = HouseBuilder()
houseBuilder.setup()
houses += houseBuilder.build()
}
fun build(): Village {
return Village(houses)
}
}
@AdvancedDSL2
fun village(setup: VillageBuilder.() -> Unit): Village {
val villageBuilder = VillageBuilder()
villageBuilder.setup()
return villageBuilder.build()
}