Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/src/main/scala/com/avsystem/commons/SharedExtensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,19 @@ object SharedExtensionsUtils extends SharedExtensions {
*/
def toOptArg: OptArg[A] =
if (tr.isFailure) OptArg.Empty else OptArg(tr.get)

/**
* Apply side-effect only if Try is a failure.
*
* Don't use .failed projection here, because it unnecessarily creates Exception in case of Success,
* which is an expensive operation.
*/
def tapFailure(action: Throwable => Unit): Try[A] = tr match {
case Success(_) => tr
case Failure(throwable) =>
action(throwable)
tr
}
}

class LazyTryOps[A](private val tr: () => Try[A]) extends AnyVal {
Expand Down