-
Notifications
You must be signed in to change notification settings - Fork 29
/
Yield.scala
38 lines (31 loc) · 1.34 KB
/
Yield.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
package com.thoughtworks.dsl.keywords
import com.thoughtworks.dsl.Dsl
import com.thoughtworks.dsl.Dsl.Keyword
import scala.concurrent.Future
import scala.language.implicitConversions
/**
* @author 杨博 (Yang Bo)
* @example This `Yield` keyword must be put inside a function that returns `Stream[Element]` or `Stream[Element] !! ...`,
* or it will not compile.
*
* {{{
* "def f(): Unit = !Yield(1)" shouldNot compile
* }}}
*
*/
final case class Yield[Element](element: Element) extends AnyVal with Keyword[Yield[Element], Unit]
object Yield {
implicit def implicitYield[Element](element: Element): Yield[Element] = Yield[Element](element)
implicit def yieldDsl[Element, That >: Element]: Dsl[Yield[Element], Stream[That], Unit] =
new Dsl[Yield[Element], Stream[That], Unit] {
def cpsApply(keyword: Yield[Element], mapper: Unit => Stream[That]): Stream[That] = {
new Stream.Cons(keyword.element, mapper(()))
}
}
implicit def futureYieldDsl[Element, That >: Element]: Dsl[Yield[Element], Stream[Future[That]], Unit] =
new Dsl[Yield[Element], Stream[Future[That]], Unit] {
def cpsApply(keyword: Yield[Element], handler: Unit => Stream[Future[That]]): Stream[Future[That]] = {
new Stream.Cons(Future.successful(keyword.element), handler(()))
}
}
}