Skip to content

Commit 2177a13

Browse files
add with function that can loaded with multiple scopes
1 parent f656117 commit 2177a13

File tree

1 file changed

+103
-0
lines changed
  • codebase/src/main/java/com/singularity_code/codebase/util/scope

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.singularity_code.codebase.util.scope
2+
3+
import kotlin.contracts.ExperimentalContracts
4+
import kotlin.contracts.InvocationKind
5+
import kotlin.contracts.contract
6+
7+
/**
8+
* Created by: stefanus
9+
* 25/09/23 01.36
10+
* Design by: [email protected]
11+
*/
12+
@OptIn(ExperimentalContracts::class)
13+
@Suppress("SUBTYPING_BETWEEN_CONTEXT_RECEIVERS")
14+
inline fun <A, B, R> with(
15+
a: A,
16+
b: B,
17+
block: context(A, B) () -> R
18+
): R {
19+
contract {
20+
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
21+
}
22+
return block(a, b)
23+
}
24+
25+
@OptIn(ExperimentalContracts::class)
26+
@Suppress("SUBTYPING_BETWEEN_CONTEXT_RECEIVERS")
27+
inline fun <A, B, C, R> with(
28+
a: A,
29+
b: B,
30+
c: C,
31+
block: context(A, B, C) () -> R
32+
): R {
33+
contract {
34+
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
35+
}
36+
return block(a, b, c)
37+
}
38+
39+
@OptIn(ExperimentalContracts::class)
40+
@Suppress("SUBTYPING_BETWEEN_CONTEXT_RECEIVERS")
41+
inline fun <A, B, C, D, R> with(
42+
a: A,
43+
b: B,
44+
c: C,
45+
d: D,
46+
block: context(A, B, C, D) () -> R
47+
): R {
48+
contract {
49+
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
50+
}
51+
return block(a, b, c, d)
52+
}
53+
54+
@OptIn(ExperimentalContracts::class)
55+
@Suppress("SUBTYPING_BETWEEN_CONTEXT_RECEIVERS")
56+
inline fun <A, B, C, D, E, R> with(
57+
a: A,
58+
b: B,
59+
c: C,
60+
d: D,
61+
e: E,
62+
block: context(A, B, C, D, E) () -> R
63+
): R {
64+
contract {
65+
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
66+
}
67+
return block(a, b, c, d, e)
68+
}
69+
70+
@OptIn(ExperimentalContracts::class)
71+
@Suppress("SUBTYPING_BETWEEN_CONTEXT_RECEIVERS")
72+
inline fun <A, B, C, D, E, F, R> with(
73+
a: A,
74+
b: B,
75+
c: C,
76+
d: D,
77+
e: E,
78+
f: F,
79+
block: context(A, B, C, D, E, F) () -> R
80+
): R {
81+
contract {
82+
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
83+
}
84+
return block(a, b, c, d, e, f)
85+
}
86+
87+
@OptIn(ExperimentalContracts::class)
88+
@Suppress("SUBTYPING_BETWEEN_CONTEXT_RECEIVERS")
89+
inline fun <A, B, C, D, E, F, G, R> with(
90+
a: A,
91+
b: B,
92+
c: C,
93+
d: D,
94+
e: E,
95+
f: F,
96+
g: G,
97+
block: context(A, B, C, D, E, F, G) () -> R
98+
): R {
99+
contract {
100+
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
101+
}
102+
return block(a, b, c, d, e, f, g)
103+
}

0 commit comments

Comments
 (0)