Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isSingletonCasesEnum flag #457

Open
wants to merge 13 commits into
base: scala3-v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import com.softwaremill.UpdateVersionInDocs
import com.softwaremill.SbtSoftwareMillCommon.commonSmlBuildSettings
import com.softwaremill.Publish.{updateDocs, ossPublishSettings}

val scala3 = "3.2.1"
val scala3 = "3.2.2"

ThisBuild / dynverTagPrefix := "scala3-v" // a custom prefix is needed to differentiate tags between scala2 & scala3 versions

Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.8.0
sbt.version=1.8.2
4 changes: 2 additions & 2 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ addSbtPlugin(
"com.softwaremill.sbt-softwaremill" % "sbt-softwaremill-publish" % sbtSoftwareMillVersion
)

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.12.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.9")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.13.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.10")
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.1.1")
1 change: 1 addition & 0 deletions src/core/impl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ trait SealedTraitDerivation:
IArray.from(anns[A]),
IArray(paramTypeAnns[A]*),
isEnum[A],
isSingletonCasesEnum[A],
IArray.from(inheritedAnns[A])
)

Expand Down
1 change: 1 addition & 0 deletions src/core/interface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ case class SealedTrait[Typeclass[_], Type](
annotations: IArray[Any],
typeAnnotations: IArray[Any],
isEnum: Boolean,
isSingletonCasesEnum: Boolean,
inheritedAnnotations: IArray[Any]
) extends Serializable:

Expand Down
9 changes: 9 additions & 0 deletions src/core/macro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ object Macro:

inline def isEnum[T]: Boolean =
${ isEnum[T] }

inline def isSingletonCasesEnum[T]: Boolean =
${ isSingletonCasesEnum[T] }

inline def anns[T]: List[Any] =
${ anns[T] }
Expand Down Expand Up @@ -57,6 +60,12 @@ object Macro:
import quotes.reflect.*

Expr(TypeRepr.of[T].typeSymbol.flags.is(Flags.Enum))

def isSingletonCasesEnum[T: Type](using Quotes): Expr[Boolean] =
import quotes.reflect.*

val ts = TypeRepr.of[T].typeSymbol
Expr(ts.flags.is(Flags.Enum) && ts.companionClass.methodMember("values").nonEmpty)

def paramTypeAnns[T: Type](using
q: Quotes
Expand Down
4 changes: 4 additions & 0 deletions src/examples/SubtypeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ trait SubtypeInfo[T] {
def traitAnnotations: Seq[Any]
def subtypeAnnotations: Seq[Seq[Any]]
def isEnum: Boolean
def isSingletonCasesEnum: Boolean
}

object SubtypeInfo extends Derivation[SubtypeInfo]:
Expand All @@ -16,6 +17,7 @@ object SubtypeInfo extends Derivation[SubtypeInfo]:
def traitAnnotations: List[Any] = Nil
def subtypeAnnotations: List[List[Any]] = Nil
def isEnum: Boolean = false
def isSingletonCasesEnum: Boolean = false

override def split[T](ctx: SealedTrait[SubtypeInfo, T]): SubtypeInfo[T] =
new SubtypeInfo[T]:
Expand All @@ -24,10 +26,12 @@ object SubtypeInfo extends Derivation[SubtypeInfo]:
def subtypeAnnotations: Seq[Seq[Any]] =
ctx.subtypes.map(_.annotations.toList).toList
def isEnum: Boolean = ctx.isEnum
def isSingletonCasesEnum: Boolean = ctx.isSingletonCasesEnum

given fallback[T]: SubtypeInfo[T] =
new SubtypeInfo[T]:
def subtypeIsObject: Seq[Boolean] = Nil
def traitAnnotations: Seq[Any] = Nil
def subtypeAnnotations: Seq[Seq[Any]] = Nil
def isEnum: Boolean = false
def isSingletonCasesEnum: Boolean = false
21 changes: 20 additions & 1 deletion src/test/SumsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ class SumsTests extends munit.FunSuite:
assertEquals(derivedSubtypeInfo.isEnum, false)
}

test("isSingletonCasesEnum field in SubtypeInfo should be true for singleton cases only enum") {
assertEquals(SubtypeInfo.derived[Size].isSingletonCasesEnum, true)
assertEquals(SubtypeInfo.derived[Planet].isSingletonCasesEnum, true)
assertEquals(SubtypeInfo.derived[ExtendingTraits].isSingletonCasesEnum, true)
}

test("isSingletonCasesEnum field in SubtypeInfo should be false for enum with non-singleton cases") {
assertEquals(SubtypeInfo.derived[Sport].isSingletonCasesEnum, false)
assertEquals(SubtypeInfo.derived[OptInt].isSingletonCasesEnum, false)
}

test("construct a Show instance for an enum") {
val res = Show.derived[Size].show(Size.S)
assertEquals(res, "S()")
Expand Down Expand Up @@ -178,7 +189,15 @@ object SumsTests:

enum Size:
case S, M, L


enum Planet(mass: Double, radius: Double):
case Earth extends Planet(5.976e+24, 6.37814e6)
case Mars extends Planet(6.421e+23, 3.3972e6)

enum OptInt:
case Some(x: Int)
case None

sealed trait Sport
case object Boxing extends Sport
case class Soccer(players: Int) extends Sport
Expand Down