some obvious stuff
Mar. 13th, 2013 11:06 am
def tryOr [T](eval: =>T, onError: Exception => T) =
try { eval } catch { case e: Exception => onError(e) }
def tryOr [T](eval: =>T, orElse: T) =
try { eval } catch { case e: Exception => orElse }
def attempt[T](eval: =>T): Option[T] = tryOr(Option(eval), None)
class BetterArray[T](array: Array[T]) {
def get(i: Int): Option[T] = attempt(array(i))
def getOrElse(i: Int, defaultValue: T) = get(i) getOrElse defaultValue
}
implicit def elementOf[T](array: Array[T]) = new BetterArray[T](array)
questions?