Created
May 22, 2020 22:09
-
-
Save dkomanov/49ecbb6211b9502ef15a8b3ae88d1fdf to your computer and use it in GitHub Desktop.
[writing-async-app-in-scala-part-1] BooleanOps3
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
implicit class FutureOfBooleanExtensions(val future: Future[Boolean]) extends AnyVal { | |
def &&(other: => BooleanOrFutureOfBoolean): Future[Boolean] = | |
v.flatMap( | |
if (_) | |
other match { | |
case Bool(otherValue) => Future.successful(otherValue) | |
case Fut(future) => future | |
} | |
else | |
Future.successful(false) | |
) | |
} | |
@implicitNotFound("This method supports only arguments of type Boolean OR Future[Boolean]") | |
sealed trait BooleanOrFutureOfBoolean | |
object BooleanOrFutureOfBoolean { | |
case class Bool(value: Boolean) extends BooleanOrFutureOfBoolean | |
case class Fut(future: Future[Boolean]) extends BooleanOrFutureOfBoolean | |
implicit def `Boolean to BooleanOrFutureOfBoolean`(v: Boolean): BooleanOrFutureOfBoolean = Bool(v) | |
implicit def `Future[Boolean] to BooleanOrFutureOfBoolean`(v: Future[Boolean]): BooleanOrFutureOfBoolean = Fut(v) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment