class Defers[T] extends AnyRef
Construction Defers: defer/recover is alternative mechanism to exception handling, simular to one in Go language.
We use one hidden in go and goScope construct are transformed to withDefers usage with help of macroses.
It is also possible to use one unsugared (as in next example), but this is a bit verbose.
def parseCsv(fname: String): Either[String, Seq[Seq[Double]]] = withDefer[Either[String,Seq[Seq[Double]]]]{ d => val in = Source.fromFile(fname) d.defer{ var r = d.recover{ case FileNotFoundException => Left("fileNotFound") } if (!r) in.close() d.recover { case ex: Throwable => Left(ex.getMessage) } } val retval:Either[String,Seq[Seq[Double]]] = Right{ for( (line, nLine) <- in.getLines.toSeq zip Stream.from(1) ) yield withDefer[Seq[Double]] { d => line.split(",") map { s=> d.defer{ d.recover{ case ex: NumberFormatException => throw new RuntimeException(s"parse error in line ${nLine} file ${fname} ") } } s.toDouble } }.toSeq } retval }
- Alphabetic
- By Inheritance
- Defers
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Instance Constructors
- new Defers()
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
def
defer(body: ⇒ Unit): Unit
can be used in main block (which can be plain or async) and store body for defered execution after evaluation of main block
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
-
def
processResult(x: Try[T]): T
called after execution of main block, where all 'defered' blocks will be executed in one thread in LIFO order.
-
def
recover(f: PartialFunction[Throwable, T]): Boolean
called inside defer blocks, where argument(t)
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
- def tryProcess(x: Try[T]): Try[T]
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )