Skip to content

Commit 8f5c1e8

Browse files
committed
Let Task.apply automatically reset
1 parent 65896cc commit 8f5c1e8

File tree

2 files changed

+13
-9
lines changed
  • domains-task

2 files changed

+13
-9
lines changed

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import org.scalatest.matchers.should.Matchers
2121
*/
2222
final class taskSpec extends AsyncFreeSpec with Matchers {
2323

24-
"tailRecurision" in Task.toFuture(*[Task] {
24+
"tailRecurision" in Task.toFuture(Task {
2525
def loop(i: Int = 0, accumulator: Int = 0): task.Task[Int] = *[task.Task] {
2626
if (i < 10000) {
2727
!Shift(loop(i + 1, accumulator + i))
@@ -34,11 +34,11 @@ final class taskSpec extends AsyncFreeSpec with Matchers {
3434
result should be(49995000)
3535
})
3636

37-
"taskToFuture" in Task.toFuture(*[Task] {
37+
"taskToFuture" in Task.toFuture(Task {
3838
succeed
3939
})
4040

41-
"loop" in Task.toFuture(*[Task] {
41+
"loop" in Task.toFuture(Task {
4242

4343
val task1: Task[Int] = Task.now(1)
4444

@@ -58,13 +58,13 @@ final class taskSpec extends AsyncFreeSpec with Matchers {
5858
a[MyException] should be thrownBy task1
5959
}
6060

61-
"try" in Task.toFuture(*[Task] {
61+
"try" in Task.toFuture(Task {
6262
class MyException extends Exception
63-
def task1: Task[Int] = *[Task] {
63+
val task1: Task[Int] = Task {
6464
throw new MyException
6565
}
6666

67-
val task2 = *[Task] {
67+
val task2 = Task {
6868
val v =
6969
try {
7070
!Shift(task1)
@@ -90,7 +90,7 @@ final class taskSpec extends AsyncFreeSpec with Matchers {
9090
throw new MyException
9191
}
9292

93-
val task2: Task[String] = *[Task] {
93+
val task2: Task[String] = Task {
9494
try {
9595
"no exception"
9696
} catch {
@@ -114,6 +114,7 @@ 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]`
117118
val task: Task[Unit] = *[Task] {
118119

119120
logs += 0
@@ -137,6 +138,7 @@ final class taskSpec extends AsyncFreeSpec with Matchers {
137138
}
138139
})
139140

141+
// TODO: Re-implement Using to support `Task{}`
140142
!Shift(*[Task] {
141143
logs += 3
142144

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package domains
33

44
import com.thoughtworks.dsl.Dsl.{!!, Continuation}
55
import com.thoughtworks.dsl.keywords.Shift
6+
import com.thoughtworks.dsl.bangnotation._
67

78
import scala.collection._
89
import scala.collection.generic.CanBuildFrom
@@ -77,8 +78,9 @@ object task {
7778
@inline
7879
def delay[A](f: () => A): Task[A] = _(f())
7980

80-
@inline
81-
def apply[A](a: => A): Task[A] = delay(() => a)
81+
inline def apply[A](inline a: A): Task[A] = { handler =>
82+
reset(handler(a))
83+
}
8284

8385
/** Returns a task that does nothing but let the succeeding tasks run on `executionContext`
8486
*

0 commit comments

Comments
 (0)