Skip to content

Commit

Permalink
Rename allSingletons to choice
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Dec 7, 2024
1 parent fe36b63 commit 670e642
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
8 changes: 4 additions & 4 deletions doc/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,8 @@ enum Language:
case En(dialect: Dialect)
case Eo
```
The `allSingletons` method returns `true` if every case in a sum type is a
singleton. This also applies to sealed traits of case objects.
The `choice` method returns `true` if every case in a sum type is a
singleton, that is a "straight choice" between them. This also applies to sealed traits of case objects.

Here is an example of its use deriving `Show`:
```scala
Expand All @@ -647,12 +647,12 @@ object Show extends Derivation[Show]:
inline def split[DerivationType: SumReflection]
: Show[DerivationType] =
value =>
inline if !allSingletons then compiletime.error("cannot derive") else
inline if !choice then compiletime.error("cannot derive") else
variant(value): [VariantType <: DerivationType] =>
variant => typeName.s+"."+variant.show
```

Note that `inline if` is used to ensure that `allSingletons` is evaluated at
Note that `inline if` is used to ensure that `choice` is evaluated at
_compiletime_, enabling the error branch (`compiletime.error`) to be retained
or eliminated. If it is retained, compilation will fail.

Expand Down
8 changes: 6 additions & 2 deletions src/core/wisteria.SumDerivationMethods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import scala.compiletime.*

trait SumDerivationMethods[TypeclassType[_]]:

@deprecated("This method has been renamed to `choice`")
transparent inline def allSingletons[DerivationType: SumReflection]: Boolean =
choice[DerivationType]

transparent inline def choice[DerivationType: SumReflection]: Boolean =
inline erasedValue[DerivationType.MirroredElemTypes] match
case _: (variantType *: variantTypes) => all[variantType, variantTypes]

Expand Down Expand Up @@ -135,7 +139,7 @@ trait SumDerivationMethods[TypeclassType[_]]:
case _ =>
inline if fallible
then raise(VariantError[DerivationType](inputLabel), Unset)(using summonInline[Tactic[VariantError]])
else throw Panic(m"Should be unreachable")
else panic(m"Should be unreachable")

private transparent inline def fold[DerivationType, VariantsType <: Tuple, LabelsType <: Tuple]
(inline sum: DerivationType, size: Int, index: Int, fallible: Boolean)
Expand Down Expand Up @@ -175,6 +179,6 @@ trait SumDerivationMethods[TypeclassType[_]]:
case _ =>
inline if fallible
then raise(VariantError[DerivationType]("".tt), Unset)(using summonInline[Tactic[VariantError]])
else throw Panic(m"Should be unreachable")
else panic(m"Should be unreachable")

inline def split[DerivationType: SumReflection]: TypeclassType[DerivationType]
2 changes: 1 addition & 1 deletion src/test/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ object Show extends Derivation[Show] {
typeName.s

inline def split[DerivationType: SumReflection]: Show[DerivationType] = value =>
inline if allSingletons then
inline if choice then
variant(value): [VariantType <: DerivationType] =>
variant => typeName.s+"."+variant.show
else
Expand Down

0 comments on commit 670e642

Please sign in to comment.