def newOrElse[T]()

in chill-scala/src/main/scala/com/twitter/chill/KryoBase.scala [138:158]


  def newOrElse[T](
      cls: Class[T],
      it: TraversableOnce[Class[T] => Try[ObjectInstantiator[T]]],
      elsefn: => ObjectInstantiator[T]
  ): ObjectInstantiator[T] =
    // Just go through and try each one,
    it.flatMap(fn => fn(cls).toOption)
      .find(_ => true) // first element in traversable once (no headOption defined.)
      .getOrElse(elsefn)

  // Use call by name:
  def forClass[T](t: Class[T])(fn: () => T): ObjectInstantiator[T] =
    new ObjectInstantiator[T] {
      override def newInstance(): T =
        try {
          fn()
        } catch {
          case x: Exception =>
            throw new KryoException("Error constructing instance of class: " + t.getName, x)
        }
    }