Last active
January 26, 2022 19:38
-
-
Save michaelahlers/032cf6ec363f2ff043a8fe09f1e28722 to your computer and use it in GitHub Desktop.
Revisions
-
michaelahlers revised this gist
Jan 26, 2022 . 1 changed file with 23 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,8 +9,17 @@ object IsAuthenticated { else Some(No) } 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. ):") } -
michaelahlers created this gist
Jan 26, 2022 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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. ):") }