-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It looks like we will need `OrElse` again.
- Loading branch information
Showing
3 changed files
with
50 additions
and
6 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cats.derived | ||
|
||
enum OrElse[+A, +B]: | ||
case Primary(value: A) | ||
case Secondary(value: () => B) | ||
|
||
final def fold[C](primary: A => C, secondary: B => C): C = this match | ||
case Primary(value) => primary(value) | ||
case Secondary(value) => secondary(value()) | ||
|
||
final def unify[C >: A](implicit ev: B <:< C): C = this match | ||
case Primary(value) => value | ||
case Secondary(value) => value() | ||
|
||
object OrElse extends OrElseLowPriority: | ||
inline given primary[A, B](using inline a: A): OrElse[A, B] = | ||
OrElse.Primary(a) | ||
|
||
private[derived] sealed abstract class OrElseLowPriority: | ||
inline given secondary[A, B](using inline b: B): OrElse[A, B] = | ||
OrElse.Secondary(() => b) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cats.derived | ||
|
||
import alleycats._ | ||
import cats._ | ||
import cats.derived.all._ | ||
|
||
class EmptyTests: | ||
case class Foo(i: Int, b: Option[IntTree]) derives Empty | ||
enum IntTree: | ||
case Leaf | ||
case Node(left: IntTree, value: Int, right: IntTree) |