Skip to content

Commit c810bf2

Browse files
committed
Polishings
1 parent dfb1713 commit c810bf2

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

tests/run/suspend-strawman-2/Async.scala

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,22 @@ object Async:
5353
checkCancellation()
5454
src.poll().getOrElse:
5555
try
56-
var result: Option[T] = None
56+
var result: Option[T] = None // Not needed if we have full continuations
5757
suspend[T, Unit]: k =>
5858
src.onComplete: x =>
5959
scheduler.schedule: () =>
6060
result = Some(x)
6161
k.resume()
6262
true // signals to `src` that result `x` was consumed
6363
result.get
64+
/* With full continuations, the try block can be written more simply as follows:
65+
66+
suspend[T, Unit]: k =>
67+
src.onComplete: x =>
68+
scheduler.schedule: () =>
69+
k.resume(x)
70+
true
71+
*/
6472
finally checkCancellation()
6573

6674
end Impl
@@ -109,11 +117,6 @@ object Async:
109117
*/
110118
abstract case class ForwardingListener[T](src: Source[?], continue: Listener[?]) extends Listener[T]
111119

112-
/** A listener for values that are processed directly in an async block.
113-
* Closures of type `T => Boolean` can be SAM converted to this type.
114-
*/
115-
abstract case class FinalListener[T](apply: T => Boolean) extends Listener[T]
116-
117120
/** An asynchronous data source. Sources can be persistent or ephemeral.
118121
* A persistent source will always pass same data to calls of `poll and `onComplete`.
119122
* An ephememral source can pass new data in every call.
@@ -158,7 +161,7 @@ object Async:
158161
/** Add `k` to the listener set of this source */
159162
protected def addListener(k: Listener[T]): Unit
160163

161-
def onComplete(k: Listener[T]): Unit = synchronized:
164+
def onComplete(k: Listener[T]): Unit =
162165
if !poll(k) then addListener(k)
163166

164167
end OriginalSource
@@ -232,8 +235,8 @@ object Async:
232235
/** If left (respectively, right) source succeeds with `x`, pass `Left(x)`,
233236
* (respectively, Right(x)) on to the continuation.
234237
*/
235-
def either[T, U](src1: Source[T], src2: Source[U]): Source[Either[T, U]] =
236-
race[Either[T, U]](src1.map(Left(_)), src2.map(Right(_)))
238+
def either[T1, T2](src1: Source[T1], src2: Source[T2]): Source[Either[T1, T2]] =
239+
race(src1.map(Left(_)), src2.map(Right(_)))
237240

238241
end Async
239242

tests/run/suspend-strawman-2/Test.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import fiberRuntime.boundary.setName
1616
val a = Future{ setName("ya"); 22 }
1717
val b = Future{ setName("yb"); 11 }
1818
a.zip(b).value
19+
val z = Future:
20+
val a = Future{ setName("za"); 22 }
21+
val b = Future{ setName("zb"); true }
22+
a.alt(b).value
23+
val _: Future[Int | Boolean] = z
1924
println("test async:")
2025
Async.blocking:
2126
println(x.value)
@@ -24,3 +29,4 @@ import fiberRuntime.boundary.setName
2429
//println(TestChoices)
2530

2631

32+

tests/run/suspend-strawman-2/futures.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ object Future:
123123
ac.root.addChild(f)
124124
f
125125

126-
extension [T1](f1: Future[T1])
126+
extension [T](f1: Future[T])
127127

128128
/** Parallel composition of two futures.
129129
* If both futures succeed, succeed with their values in a pair. Otherwise,
130130
* fail with the failure that was returned first and cancel the other.
131131
*/
132-
def zip[T2](f2: Future[T2])(using Async.Config): Future[(T1, T2)] = Future:
132+
def zip[U](f2: Future[U])(using Async.Config): Future[(T, U)] = Future:
133133
Async.await(Async.either(f1, f2)) match
134134
case Left(Success(x1)) => (x1, f2.value)
135135
case Right(Success(x2)) => (f1.value, x2)
@@ -140,7 +140,7 @@ object Future:
140140
* If either task succeeds, succeed with the success that was returned first
141141
* and cancel the other. Otherwise, fail with the failure that was returned last.
142142
*/
143-
def alt[T2 >: T1](f2: Future[T2], name: String = "alt")(using Async.Config): Future[T2] = Future:
143+
def alt(f2: Future[T], name: String = "alt")(using Async.Config): Future[T] = Future:
144144
boundary.setName(name)
145145
Async.await(Async.either(f1, f2)) match
146146
case Left(Success(x1)) => f2.cancel(); x1

0 commit comments

Comments
 (0)