Skip to content

Instantly share code, notes, and snippets.

@michaelahlers
Last active January 26, 2022 19:38
Show Gist options
  • Select an option

  • Save michaelahlers/032cf6ec363f2ff043a8fe09f1e28722 to your computer and use it in GitHub Desktop.

Select an option

Save michaelahlers/032cf6ec363f2ff043a8fe09f1e28722 to your computer and use it in GitHub Desktop.

Revisions

  1. michaelahlers revised this gist Jan 26, 2022. 1 changed file with 23 additions and 2 deletions.
    25 changes: 23 additions & 2 deletions boolean-to-domain.scala
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,17 @@ object IsAuthenticated {
    else Some(No)
    }

    case object Yes extends IsAuthenticated
    case object No extends IsAuthenticated
    case object Yes extends IsAuthenticated {
    def unapply(x: JavaApi): Option[Yes.type] =
    if (x.isAuthenticated) Some(Yes)
    else None
    }

    case object No extends IsAuthenticated {
    def unapply(x: JavaApi): Option[No.type] =
    if (x.isAuthenticated) None
    else Some(No)
    }

    def x: JavaApi =
    new JavaApi {
    @@ -28,8 +37,20 @@ x match {
    case IsAuthenticated(No) => println("No. ):")
    }

    // YES!
    x match {
    case Yes(_) => println("YES!")
    case No(_) => println("No. ):")
    }

    // No. ):
    y match {
    case IsAuthenticated(Yes) => println("YES!")
    case IsAuthenticated(No) => println("No. ):")
    }

    // No. ):
    y match {
    case Yes(_) => println("YES!")
    case No(_) => println("No. ):")
    }
  2. michaelahlers created this gist Jan 26, 2022.
    35 changes: 35 additions & 0 deletions boolean-to-domain.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    trait JavaApi {
    def isAuthenticated: Boolean
    }

    sealed trait IsAuthenticated
    object IsAuthenticated {
    def unapply(x: JavaApi): Option[IsAuthenticated] =
    if (x.isAuthenticated) Some(Yes)
    else Some(No)
    }

    case object Yes extends IsAuthenticated
    case object No extends IsAuthenticated

    def x: JavaApi =
    new JavaApi {
    override def isAuthenticated = true
    }

    def y: JavaApi =
    new JavaApi {
    override def isAuthenticated = false
    }

    // YES!
    x match {
    case IsAuthenticated(Yes) => println("YES!")
    case IsAuthenticated(No) => println("No. ):")
    }

    // No. ):
    y match {
    case IsAuthenticated(Yes) => println("YES!")
    case IsAuthenticated(No) => println("No. ):")
    }