Skip to content

Commit ad734cf

Browse files
authored
Merge pull request #534 from Atry/task
Let Task support ToView.FromIterable
2 parents 793998d + 6eb5bc8 commit ad734cf

File tree

4 files changed

+29
-17
lines changed
  • domains-Continuation/src/main/scala/com/thoughtworks/dsl/domains
  • domains-Task
  • keywords-Await/src/main/scala/com/thoughtworks/dsl/keywords

4 files changed

+29
-17
lines changed

domains-Continuation/src/main/scala/com/thoughtworks/dsl/domains/Continuation.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package domains
33

44
import scala.util._
55
import scala.util.control.NonFatal
6+
import com.thoughtworks.dsl.keywords.Pure
67

78
type Continuation[R, +A] = (A => R) => R
89

@@ -21,7 +22,10 @@ object Continuation {
2122
def delay[R, A](a: () => A): R !! A = _(a())
2223

2324
inline def apply[R, A](inline a: A): R !! A = { handler =>
24-
reset(handler(a))
25+
reset {
26+
// !Pure ensures stack safety
27+
handler(!Pure(a))
28+
}
2529
}
2630

2731
def toTryContinuation[LeftDomain, Value](

domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/taskSpec.scala

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ import scala.language.implicitConversions
99
import com.thoughtworks.dsl.keywords.{Using, ToView}
1010
import com.thoughtworks.dsl.domains._
1111
import com.thoughtworks.dsl.keywords.Shift
12-
import com.thoughtworks.dsl.keywords.Shift.given
1312

1413
import scala.collection.mutable.ArrayBuffer
1514
import scala.util.control.TailCalls
1615
import scala.util.{Failure, Success}
1716
import org.scalatest.freespec.AsyncFreeSpec
1817
import org.scalatest.matchers.should.Matchers
1918

20-
/** @author 杨博 (Yang Bo)
19+
/** @author
20+
* 杨博 (Yang Bo)
2121
*/
2222
final class taskSpec extends AsyncFreeSpec with Matchers {
2323

2424
"tailRecurision" in Task.toFuture(Task {
25-
def loop(i: Int = 0, accumulator: Int = 0): Task[Int] = *[Task] {
25+
def loop(i: Int = 0, accumulator: Int = 0): Task[Int] = Task {
2626
if (i < 10000) {
2727
!Shift(loop(i + 1, accumulator + i))
2828
} else {
@@ -114,8 +114,8 @@ final class taskSpec extends AsyncFreeSpec with Matchers {
114114
"autoClose" in {
115115
val logs = ArrayBuffer.empty[Int]
116116

117-
// TODO: Re-implement Using to support `Task {}` instead of `*[Task]`
118-
val task: Task[Unit] = *[Task] {
117+
// TODO: Re-implement Using to support `Task {}` instead of `Task`
118+
val task: Task[Unit] = Task {
119119

120120
logs += 0
121121

@@ -139,7 +139,7 @@ final class taskSpec extends AsyncFreeSpec with Matchers {
139139
})
140140

141141
// TODO: Re-implement Using to support `Task{}`
142-
!Shift(*[Task] {
142+
!Shift(Task {
143143
logs += 3
144144

145145
!Using(new AutoCloseable {
@@ -169,21 +169,24 @@ final class taskSpec extends AsyncFreeSpec with Matchers {
169169
}
170170

171171
Task.toFuture(task).map { _ =>
172-
logs should be(ArrayBuffer(0, 10, 11, 12, 3, 40, 41, 42, 6, 52, 51, 50, 7, 22, 21, 20))
172+
logs should be(
173+
ArrayBuffer(0, 10, 11, 12, 3, 40, 41, 42, 6, 52, 51, 50, 7, 22, 21, 20)
174+
)
173175
}
174176

175177
}
176178

177179
// Task.join is not supported any more
178180
"nested seq of task" ignore {
179181

180-
def composeTask(t0: Task[Seq[Task[Seq[Task[Seq[Task[Seq[Float]]]]]]]]): Task[Seq[Seq[Seq[Seq[Float]]]]] = {
181-
// TODO: remove explicit type parameters when https://github.com/scala/bug/issues/11068 is fixed
182-
*[Task]/*.join*/ apply Seq {
182+
def composeTask(
183+
t0: Task[Seq[Task[Seq[Task[Seq[Task[Seq[Float]]]]]]]]
184+
): Task[Seq[Seq[Seq[Seq[Float]]]]] = {
185+
Task /*.join*/ apply Seq {
183186
val t1 = !ToView.FromIterable(!Shift(t0))
184-
!Shift(*[Task]/*.join*/ apply Seq {
187+
!Shift(Task /*.join*/ apply Seq {
185188
val t2 = !ToView.FromIterable(!Shift(t1))
186-
!Shift(*[Task]/*.join*/ apply Seq {
189+
!Shift(Task /*.join*/ apply Seq {
187190
val t3 = !ToView.FromIterable(!Shift(t2))
188191
!Shift(t3)
189192
})

domains-Task/src/main/scala/com/thoughtworks/dsl/domains/Task.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.thoughtworks.dsl
22
package domains
33

44
import com.thoughtworks.dsl.domains.Continuation, Continuation.!!
5+
import com.thoughtworks.dsl.keywords.Pure
6+
import com.thoughtworks.dsl.keywords.Suspend
57
import com.thoughtworks.dsl.keywords.Shift
68
import com.thoughtworks.dsl.reset, reset._
79

@@ -78,7 +80,10 @@ object Task extends TaskPlatformSpecificFunctions {
7880
def delay[A](f: () => A): Task[A] = _(f())
7981

8082
inline def apply[A](inline a: A): Task[A] = { handler =>
81-
reset(handler(a))
83+
reset {
84+
// !Pure ensures stack safety
85+
handler(!Pure(a))
86+
}
8287
}
8388

8489
/** Returns a task that does nothing but let the succeeding tasks run on `executionContext`
@@ -90,7 +95,7 @@ object Task extends TaskPlatformSpecificFunctions {
9095
* import org.scalatest.Assertion
9196
* import scala.concurrent.ExecutionContext
9297
* import com.thoughtworks.dsl.keywords.Shift
93-
* def myTask: Task[Assertion] = *[Task] {
98+
* def myTask: Task[Assertion] = Task {
9499
* val originalThread = Thread.currentThread
95100
* !Shift(Task.switchExecutionContext(ExecutionContext.global))
96101
* Thread.currentThread should not be originalThread

keywords-Await/src/main/scala/com/thoughtworks/dsl/keywords/Await.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import scala.language.implicitConversions
3636
* {{{
3737
* import com.thoughtworks.dsl.domains.Task
3838
* import com.thoughtworks.dsl.keywords.Await
39-
* val myTask = *[Task] {
39+
* val myTask = Task {
4040
* !Await(myFuture42)
4141
* }
4242
* }}}
@@ -45,7 +45,7 @@ import scala.language.implicitConversions
4545
* [[domains.task.Task.toFuture]].
4646
*
4747
* {{{
48-
* val myAssertionTask = *[Task] {
48+
* val myAssertionTask = Task {
4949
* !Shift(myTask) should be(42)
5050
* }
5151
* Task.toFuture(myAssertionTask)

0 commit comments

Comments
 (0)