in src/main/scala/com/twitter/stitch/Arrow.scala [1179:1237]
private[this] def isConstant(i: Int): Boolean =
constants != null && constants(i) != null
override def run[V](t: Try[T], l: Locals, tail: Arrow[Seq[U], V]): Stitch[V] =
tail(Stitch.traverse(as)(a => a.run(t, l)), l)
override def run[T2 <: T, V](
ts: ArrayBuffer[Try[T2]],
ls: ArrayBuffer[Locals],
tail: Arrow[Seq[U], V]
): Stitch[ArrayBuffer[Try[V]]] = {
Stitch
.traverse(as) {
case Arrow.Const(Return(_)) => Stitch.value(null)
case a => a.run(TryBuffer.copy(ts), ls)
}
.flatMap { tss => // tss(j) is null if the jth arrow is constant
// it's safe to reuse the first non-null tss(j) because it's uniquely owned
val ts = tss.find(_ != null).get.asInstanceOf[ArrayBuffer[Try[Seq[U]]]]
var i = 0
while (i < ts.length) {
// check for Throw
var t: Try[U] = null
var j = 0
while (t == null && j < as.length) {
if (!isConstant(j)) {
tss(j)(i) match {
case Return(_) =>
case tt => t = tt
}
}
j += 1
}
if (t != null) {
ts(i) = t.asInstanceOf[Try[Seq[U]]]
// otherwise collect the Returns
} else {
val us = new ArrayBuffer[U](as.length)
j = 0
while (j < as.length) {
if (isConstant(j))
us += constants(j)
else
tss(j)(i) match {
case Return(u) => us += u
case _ => scala.sys.error("bug")
}
j += 1
}
ts(i) = Return(us)
}
i += 1
}
tail.run(ts, ls)
}
}