-
Notifications
You must be signed in to change notification settings - Fork 1k
/
implicitFuns.scala
263 lines (200 loc) · 6.24 KB
/
implicitFuns.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// scalajs: --skip --pending
object Test {
def main(args: Array[String]): Unit = {
implicit val world: String = "world!"
val i1 = ((s: String) ?=> s.length > 2)
val i2 = {(s: String) ?=> s.length > 2}
assert(i1)
assert(i2)
val x: String ?=> Boolean = { (s: String) ?=> s.length > 2 }
val xx: (String, Int) ?=> Int = (x: String, y: Int) ?=> x.length + y
val y: String => Boolean = x(using _)
object nested {
implicit val empty: String = ""
assert(!x)
}
val yy: (String, Int) => Any = xx(using _, _)
val z1: String ?=> Boolean = implicitly[String].length >= 2
assert(z1)
type StringlyBool = String ?=> Boolean
val z2: StringlyBool = implicitly[String].length >= 2
assert(z2)
type Stringly[T] = String ?=> T
val z3: Stringly[Boolean] = implicitly[String].length >= 2
assert(z3)
type GenericImplicit[X] = X ?=> Boolean
val z4: GenericImplicit[String] = implicitly[String].length >= 2
assert(z4)
val b = x(using "hello")
val b1: Boolean = b
val bi = x
val bi1: Boolean = bi
val c = xx(using "hh", 22)
val c1: Int = c
Contextual.main(args)
def foo(s: String): Stringly[Int] = 42
//(if ("".isEmpty) foo("") else foo("")).apply given "" // does not typecheck
}
}
object Contextual {
class Key[+V]
class Context(bindings: Map[Key[Any], Any]) {
def binding[V](key: Key[V]): Option[V] =
bindings.get(key).asInstanceOf[Option[V]]
def withBinding[V](key: Key[V], value: V): Context =
new Context(bindings + ((key, value)))
}
val rootContext = new Context(Map())
val Source = new Key[String]
val Options = new Key[List[String]]
type Ctx[T] = Context ?=> T
def ctx: Ctx[Context] = implicitly[Context]
def compile(s: String): Ctx[Boolean] =
runOn(new java.io.File(s))(using ctx.withBinding(Source, s)) >= 0
def runOn(f: java.io.File): Ctx[Int] = {
val options = List("-verbose", "-explaintypes")
process(f).apply(using ctx.withBinding(Options, options))
}
def process(f: java.io.File): Ctx[Int] =
ctx.binding(Source).get.length - ctx.binding(Options).get.length
def main(args: Array[String]) = {
implicit val context: Context = rootContext
assert(compile("abc"))
assert(compile("ab"))
assert(!compile("a"))
}
}
import collection.mutable.ListBuffer
class Transaction {
private val log = new ListBuffer[String]
def println(s: String): Unit = log += s
private var aborted = false
private var committed = false
def abort(): Unit = { aborted = true }
def isAborted = aborted
def commit(): Unit =
if (!aborted && !committed) {
Console.println("******* log ********")
log.foreach(Console.println)
committed = true
}
}
object TransactionalExplicit {
def transaction[T](op: Transaction => T) = {
val trans: Transaction = new Transaction
op(trans)
trans.commit()
}
def f1(x: Int)(implicit thisTransaction: Transaction): Int = {
thisTransaction.println(s"first step: $x")
f2(x + 1)
}
def f2(x: Int)(implicit thisTransaction: Transaction): Int = {
thisTransaction.println(s"second step: $x")
f3(x * x)
}
def f3(x: Int)(implicit thisTransaction: Transaction): Int = {
thisTransaction.println(s"third step: $x")
if (x % 2 != 0) thisTransaction.abort()
x
}
def main(args: Array[String]) = {
transaction {
implicit thisTransaction =>
val res = f1(args.length)
println(if (thisTransaction.isAborted) "aborted" else s"result: $res")
}
}
}
object Transactional {
type Transactional[T] = Transaction ?=> T
def transaction[T](op: Transactional[T]) = {
implicit val trans: Transaction = new Transaction
op
trans.commit()
}
def thisTransaction: Transactional[Transaction] = implicitly[Transaction]
def f1(x: Int): Transactional[Int] = {
thisTransaction.println(s"first step: $x")
f2(x + 1)
}
def f2(x: Int): Transactional[Int] = {
thisTransaction.println(s"second step: $x")
f3(x * x)
}
def f3(x: Int): Transactional[Int] = {
thisTransaction.println(s"third step: $x")
if (x % 2 != 0) thisTransaction.abort()
x
}
def main(args: Array[String]) = {
transaction {
val res = f1(args.length)
println(if (thisTransaction.isAborted) "aborted" else s"result: $res")
}
}
}
object TransactionalExpansion {
def transaction[T](op: Transaction => T) = {
val trans: Transaction = new Transaction
op.apply(trans)
trans.commit()
}
def thisTransaction = ($t: Transaction) => $t
def f1(x: Int) = { ($t: Transaction) =>
thisTransaction.apply($t).println(s"first step: $x")
f2(x + 1).apply($t)
}
def f2(x: Int) = { ($t: Transaction) =>
thisTransaction.apply($t).println(s"second step: $x")
f3(x * x).apply($t)
}
def f3(x: Int) = { ($t: Transaction) =>
thisTransaction.apply($t).println(s"third step: $x")
if (x % 2 != 0) thisTransaction.apply($t).abort()
x
}
def main(args: Array[String]) = {
transaction { $t =>
val res = f1(args.length).apply($t)
println(if (thisTransaction.apply($t).isAborted) "aborted" else s"result: $res")
}
}
}
object TransactionalAbstracted {
type Transactional[T] = Transaction ?=> T
trait TransOps {
def thisTransaction: Transactional[Transaction]
def f1(x: Int): Transactional[Int]
def f2(x: Int): Transactional[Int]
def f3(x: Int): Transactional[Int]
}
object TransOpsObj extends TransOps {
def thisTransaction: Transactional[Transaction] = implicitly[Transaction]
def f1(x: Int): Transactional[Int] = {
thisTransaction.println(s"first step: $x")
f2(x + 1)
}
def f2(x: Int): Transactional[Int] = {
thisTransaction.println(s"second step: $x")
f3(x * x)
}
def f3(x: Int): Transactional[Int] = {
thisTransaction.println(s"third step: $x")
if (x % 2 != 0) thisTransaction.abort()
x
}
}
val transOps: TransOps = TransOpsObj
def transaction[T](op: Transactional[T]) = {
implicit val trans: Transaction = new Transaction
op
trans.commit()
}
def main(args: Array[String]) = {
transaction {
val res = transOps.f1(args.length)
println(if (transOps.thisTransaction.isAborted) "aborted" else s"result: $res")
}
}
}