Created
August 25, 2018 04:48
-
-
Save vi-kas/6d7dc93d33a20322ea417756c4aeeb74 to your computer and use it in GitHub Desktop.
Ways we can use `outcomeOf` method from trait `OutcomeOf` at usage places.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package learning.gists | |
/** | |
* GIST shows - | |
* Ways we can use `outcomeOf` method at usage place from trait `OutcomeOf`. | |
* 1. We can mixin the trait. | |
* 2. We can import trait's comp object. | |
*/ | |
//Outcome ADT | |
sealed trait Outcome | |
case object Succeeded extends Outcome | |
case object Cancelled extends Outcome | |
case object Failed extends Outcome | |
//Just a trait with one method outcomeOf which executes func passed. | |
trait OutcomeOf { | |
def outcomeOf(f: => Any): Outcome = { | |
try{ | |
f | |
Succeeded | |
}catch { | |
case _: Exception => Cancelled | |
} | |
} | |
} | |
/** | |
* Companion object that facilitates the importing of OutcomeOf's `outcomeOf` method as | |
* an alternative to mixing it in. | |
* | |
* One use case is to import OutcomeOf's method so we can use | |
* it in the Scala interpreter. | |
*/ | |
object OutcomeOf extends OutcomeOf | |
/*package usage1 { | |
/** | |
* UsageApp1 shows mixin of `OutcomeOf` to use `outcomeOf` method. | |
*/ | |
object UsageApp1 extends App with OutcomeOf { | |
outcomeOf(println("Yo!")) | |
} | |
}*/ | |
package usage2 { | |
// importing comp object OutcomeOf's methods. | |
import learning.gists.OutcomeOf._ | |
/** | |
* UsageApp1 shows import of `OutcomeOf` comp object to use `outcomeOf` method. | |
*/ | |
object UsageApp2 extends App { | |
outcomeOf(println("Yo!")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment