diff options
author | Philipp Haller <hallerp@gmail.com> | 2013-05-23 15:52:31 +0200 |
---|---|---|
committer | Philipp Haller <hallerp@gmail.com> | 2013-05-23 15:52:31 +0200 |
commit | 8b740b436a3de111269e004e595fda70f6533704 (patch) | |
tree | 41ec001a0e5eb72940d0c32473121775d3220d8a /src/main/scala/scala/async/iterators/IteratorState.scala | |
parent | ffab92802c130c7e881440d360e7cef051b7f3d4 (diff) | |
download | scala-async-topic/wip-futuresystem-extension.tar.gz scala-async-topic/wip-futuresystem-extension.tar.bz2 scala-async-topic/wip-futuresystem-extension.zip |
Enable generalized state machinestopic/wip-futuresystem-extension
- async { } block can now generate a subclass of an existing trait
- future system can directly return the state machine instance
from async, without spawning an actual future
- adds iterator-based async implementation with future system
Diffstat (limited to 'src/main/scala/scala/async/iterators/IteratorState.scala')
-rw-r--r-- | src/main/scala/scala/async/iterators/IteratorState.scala | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/main/scala/scala/async/iterators/IteratorState.scala b/src/main/scala/scala/async/iterators/IteratorState.scala new file mode 100644 index 0000000..5b8cf06 --- /dev/null +++ b/src/main/scala/scala/async/iterators/IteratorState.scala @@ -0,0 +1,50 @@ +package scala.async.iterators + +import scala.util.{Try, Success, Failure} + +trait IteratorState[T] { + + def apply(v: Try[Any]): Unit + def apply: Unit + def `result$async` : IteratorState[T] + + private[this] var _value: Option[T] = None + private[this] var _exc: Throwable = null + + def result_= (value: T) = + _value = Some(value) + + def result: T = { + if (_exc != null) + throw _exc + else + _value.get + } + + def exception_= (exc: Throwable) = + _exc = exc + + def exception: Throwable = + _exc + + def isFailed: Boolean = + _exc != null + + def onComplete(cont: IteratorState[_]) = { + // cont will always be `this` + /* do nothing */ + } + + def next: T = { + // continue iteration with next state + this.apply(Success(result)) + // return current result + result + } + + def hasNext: Boolean = { + println("invoking apply") + apply + _value.nonEmpty + } +} |