diff --git a/doc/basics.md b/doc/basics.md index 640f910..c4cb91e 100644 --- a/doc/basics.md +++ b/doc/basics.md @@ -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 @@ -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. diff --git a/src/core/wisteria.SumDerivationMethods.scala b/src/core/wisteria.SumDerivationMethods.scala index fcf1a27..45adedf 100644 --- a/src/core/wisteria.SumDerivationMethods.scala +++ b/src/core/wisteria.SumDerivationMethods.scala @@ -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] @@ -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) @@ -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] diff --git a/src/test/tests.scala b/src/test/tests.scala index b2a50cc..3bed4df 100644 --- a/src/test/tests.scala +++ b/src/test/tests.scala @@ -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