@@ -16,116 +16,103 @@ import scala.util.control.NonFatal
16
16
* support [[scalaz.Bind ]], [[scalaz.MonadError ]] and [[scalaz.MonadTrans ]].
17
17
*
18
18
* @example
19
- * [[scalaz.Free. Trampoline ]] is a monadic data type that performs tail call
19
+ * [[scalaz.Free# Trampoline ]] is a monadic data type that performs tail call
20
20
* optimization. It can be built from a `@[[Dsl.reset reset ]]` code block
21
21
* within some [[Dsl.Keyword#unary_$bang !-notation ]], similar to the
22
22
* [[com.thoughtworks.each.Monadic.EachOps#each each ]] method in
23
23
* [[https://github.com/ThoughtWorksInc/each ThoughtWorks Each ]].
24
- *
25
- * {{{
26
- * import _root_.scalaz.Trampoline
27
- * import _root_.scalaz.Free.Trampoline
28
- * import com.thoughtworks.dsl.keywords.Monadic
29
- * import com.thoughtworks.dsl.domains.scalaz.given
30
- * import com.thoughtworks.dsl.macros.Reset.Default.reset
31
- * import com.thoughtworks.dsl.keywords.Monadic.unary_!
32
- *
33
- * val trampoline3 = Trampoline.done(3)
34
- *
35
- * def dslSquare = reset(Trampoline.delay {
36
- * s"This string is produced by a trampoline: ${!trampoline3 * !trampoline3}"
37
- * })
38
- *
39
- * dslSquare.run should be("This string is produced by a trampoline: 9")
40
- * }}}
41
- *
42
- * `!trampoline3` is a shortcut of `!Monadic(trampoline3)`, enabled by `import
43
- * com.thoughtworks.dsl.keywords.Monadic.given`, which will be converted to
44
- * `flatMap` calls by our DSL interpreter. Thus, the method `dslSquare` is
45
- * equivalent to the following code in [[scalaz.syntax ]]:
46
- *
47
- * {{{
48
- *
49
- * def scalazSyntaxSquare = trampoline3.flatMap { tmp1 =>
50
- * trampoline3.flatMap { tmp2 =>
51
- * Trampoline.delay {
52
- * s"This string is produced by a trampoline: ${tmp1 * tmp2}"
53
- * }
54
- * }
55
- * }
56
- *
57
- * scalazSyntaxSquare.run should be("This string is produced by a trampoline: 9")
58
- * }}}
59
- *
60
- * <hr/>
61
- *
62
- * A `@[[Dsl.reset reset ]]` code block can contain `try` / `catch` / `finally`
63
- * if the monadic data type supports [[scalaz.MonadError ]].
64
- *
65
- * [[https://github.com/ThoughtWorksInc/tryt.scala tryt.scala ]] is a monad
66
- * transformer that provides [[scalaz.MonadError ]], therefore `try` / `catch` /
67
- * `finally` expressions can be used inside a `@[[Dsl.reset reset ]]` code block
68
- * whose return type is `TryT[Trampoline, ?]`.
69
- *
70
- * {{{
71
- * import com.thoughtworks.tryt.invariant.TryT, TryT.given
72
- * import scala.util.{Try, Success}
73
- * type TryTTransfomredTrampoline[A] = TryT[Trampoline, A]
74
- *
75
- * val trampolineSuccess0: TryTTransfomredTrampoline[Int] = TryT(Trampoline.done(Try(0)))
76
- *
77
- * def dslTryCatch: TryTTransfomredTrampoline[String] = reset(TryT(Trampoline.delay(Try {
78
- * try {
79
- * s"Division result: ${!trampoline3 / !trampolineSuccess0}"
80
- * } catch {
81
- * case e: ArithmeticException =>
82
- * s"Cannot divide ${!trampoline3} by ${!trampolineSuccess0}"
83
- * }
84
- * })))
85
- *
86
- * inside(dslTryCatch) {
87
- * case TryT(trampoline) =>
88
- * trampoline.run should be(Success("Cannot divide 3 by 0"))
89
- * }
90
- * }}}
91
- *
92
- * Note that [[Dsl.Keyword#unary_$bang !-notation ]] can be used on both
93
- * `trampoline3` and `trampolineSuccess0` even when they are different types,
94
- * i.e. `trampoline3` is a vanilla [[scalaz.Free.Trampoline Trampoline ]], while
95
- * `trampolineSuccess0` is a
96
- * [[com.thoughtworks.tryt.invariant.TryT TryT ]]-transfomred
97
- * [[scalaz.Free.Trampoline Trampoline ]]. It is possible because the
98
- * interpreters of the [[keywords.Monadic ]] invoke [[scalaz.MonadTrans.liftM ]]
99
- * automatically.
100
- *
101
- * The above `dslTryCatch` method is equivalent to the following code in
102
- * [[scalaz.syntax ]]:
103
- *
104
- * {{{
105
- * import _root_.scalaz.syntax.monad._
106
- * def scalazSyntaxTryCatch: TryTTransfomredTrampoline[String] = {
107
- * import _root_.scalaz.syntax.monadError._
108
- * trampoline3.liftM[TryT].flatMap { tmp0 =>
109
- * trampolineSuccess0.flatMap { tmp1 =>
110
- * TryT(Trampoline.delay(Try(s"Division result: ${tmp0 / tmp1}")))
111
- * }
112
- * }.handleError {
113
- * case e: ArithmeticException =>
114
- * trampoline3.liftM[TryT].flatMap { tmp2 =>
115
- * trampolineSuccess0.flatMap { tmp3 =>
116
- * TryT(Trampoline.delay(Try(s"Cannot divide ${tmp2} by ${tmp3}")))
117
- * }
118
- * }
119
- * case e =>
120
- * e.raiseError[TryTTransfomredTrampoline, String]
121
- * }
122
- * }
123
- *
124
- * inside(scalazSyntaxTryCatch) {
125
- * case TryT(trampoline) =>
126
- * trampoline.run should be(Success("Cannot divide 3 by 0"))
24
+ * {{{
25
+ * import _root_.scalaz.Trampoline
26
+ * import _root_.scalaz.Free.Trampoline
27
+ * import com.thoughtworks.dsl.keywords.Monadic
28
+ * import com.thoughtworks.dsl.domains.scalaz.given
29
+ * import com.thoughtworks.dsl.macros.Reset.Default.reset
30
+ * import com.thoughtworks.dsl.keywords.Monadic.unary_!
31
+ *
32
+ * val trampoline3 = Trampoline.done(3)
33
+ *
34
+ * def dslSquare = reset(Trampoline.delay {
35
+ * s"This string is produced by a trampoline: ${!trampoline3 * !trampoline3}"
36
+ * })
37
+ *
38
+ * dslSquare.run should be("This string is produced by a trampoline: 9")
39
+ * }}}
40
+ * `!trampoline3` is a shortcut of `!Monadic(trampoline3)`, enabled by
41
+ * `import com.thoughtworks.dsl.keywords.Monadic.given`, which will be
42
+ * converted to `flatMap` calls by our DSL interpreter. Thus, the method
43
+ * `dslSquare` is equivalent to the following code in [[scalaz.syntax ]]:
44
+ * {{{
45
+ * def scalazSyntaxSquare = trampoline3.flatMap { tmp1 =>
46
+ * trampoline3.flatMap { tmp2 =>
47
+ * Trampoline.delay {
48
+ * s"This string is produced by a trampoline: ${tmp1 * tmp2}"
49
+ * }
50
+ * }
51
+ * }
52
+ *
53
+ * scalazSyntaxSquare.run should be("This string is produced by a trampoline: 9")
54
+ * }}}
55
+ * A `@[[Dsl.reset reset ]]` code block can contain `try` / `catch` /
56
+ * `finally` if the monadic data type supports [[scalaz.MonadError ]].
57
+ * [[https://github.com/ThoughtWorksInc/tryt.scala tryt.scala ]] is a monad
58
+ * transformer that provides [[scalaz.MonadError ]], therefore `try` / `catch`
59
+ * / `finally` expressions can be used inside a `@[[Dsl.reset reset ]]` code
60
+ * block whose return type is `TryT[Trampoline, ?]`.
61
+ * {{{
62
+ * import com.thoughtworks.tryt.invariant.TryT, TryT.given
63
+ * import scala.util.{Try, Success}
64
+ * type TryTTransfomredTrampoline[A] = TryT[Trampoline, A]
65
+ *
66
+ * val trampolineSuccess0: TryTTransfomredTrampoline[Int] = TryT(Trampoline.done(Try(0)))
67
+ *
68
+ * def dslTryCatch: TryTTransfomredTrampoline[String] = reset(TryT(Trampoline.delay(Try {
69
+ * try {
70
+ * s"Division result: ${!trampoline3 / !trampolineSuccess0}"
71
+ * } catch {
72
+ * case e: ArithmeticException =>
73
+ * s"Cannot divide ${!trampoline3} by ${!trampolineSuccess0}"
74
+ * }
75
+ * })))
76
+ *
77
+ * inside(dslTryCatch) {
78
+ * case TryT(trampoline) =>
79
+ * trampoline.run should be(Success("Cannot divide 3 by 0"))
80
+ * }
81
+ * }}}
82
+ * Note that [[Dsl.Keyword#unary_$bang !-notation ]] can be used on both
83
+ * `trampoline3` and `trampolineSuccess0` even when they are different types,
84
+ * i.e. `trampoline3` is a vanilla [[scalaz.Free.Trampoline Trampoline ]], while
85
+ * `trampolineSuccess0` is a
86
+ * [[com.thoughtworks.tryt.invariant.TryT TryT ]]-transfomred
87
+ * [[scalaz.Free.Trampoline Trampoline ]]. It is possible because the
88
+ * interpreters of the [[keywords.Monadic ]] invoke [[scalaz.MonadTrans.liftM ]]
89
+ * automatically. <p> The above `dslTryCatch` method is equivalent to the
90
+ * following code in [[scalaz.syntax ]]:
91
+ * {{{
92
+ * import _root_.scalaz.syntax.monad._
93
+ * def scalazSyntaxTryCatch: TryTTransfomredTrampoline[String] = {
94
+ * import _root_.scalaz.syntax.monadError._
95
+ * trampoline3.liftM[TryT].flatMap { tmp0 =>
96
+ * trampolineSuccess0.flatMap { tmp1 =>
97
+ * TryT(Trampoline.delay(Try(s"Division result: ${tmp0 / tmp1}")))
98
+ * }
99
+ * }.handleError {
100
+ * case e: ArithmeticException =>
101
+ * trampoline3.liftM[TryT].flatMap { tmp2 =>
102
+ * trampolineSuccess0.flatMap { tmp3 =>
103
+ * TryT(Trampoline.delay(Try(s"Cannot divide ${tmp2} by ${tmp3}")))
127
104
* }
128
- * }}}
105
+ * }
106
+ * case e =>
107
+ * e.raiseError[TryTTransfomredTrampoline, String]
108
+ * }
109
+ * }
110
+ *
111
+ * inside(scalazSyntaxTryCatch) {
112
+ * case TryT(trampoline) =>
113
+ * trampoline.run should be(Success("Cannot divide 3 by 0"))
114
+ * }
115
+ * }}}
129
116
*
130
117
* @author
131
118
* 杨博 (Yang Bo)
0 commit comments