-
Notifications
You must be signed in to change notification settings - Fork 29
/
Yield.scala
29 lines (24 loc) · 970 Bytes
/
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
package com.thoughtworks.dsl.keywords
import com.thoughtworks.dsl.Dsl
import com.thoughtworks.dsl.Dsl.Keyword
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]: Dsl[Yield[Element], Stream[Element], Unit] =
new Dsl[Yield[Element], Stream[Element], Unit] {
def interpret(keyword: Yield[Element], mapper: Unit => Stream[Element]): Stream[Element] = {
new Stream.Cons(keyword.element, mapper(()))
}
}
}