Skip to content

Commit

Permalink
mend
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnhoekstra committed Jun 23, 2024
1 parent c7e52d0 commit a1cde16
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
17 changes: 9 additions & 8 deletions macros/src/main/scala-3/enumeratum/ValueEnumMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,13 @@ In SBT settings:

val valueRepr = TypeRepr.of[ValueType]

val valueParamIndex = tpeSym.primaryConstructor.paramSymss.flatten.zipWithIndex.collectFirst {
case (p, i) if p.name == "value" => i
}
val valueParamIndex = tpeSym.primaryConstructor.paramSymss
.filterNot(_.exists(_.isType))
.flatten
.zipWithIndex
.collectFirst {
case (p, i) if p.name == "value" => i
}

type IsValue[T <: ValueType] = T

Expand Down Expand Up @@ -175,11 +179,8 @@ In SBT settings:
.flatMap(_.lift(valueParamIndex.getOrElse(-1)).collect { case ConstVal(const) =>
const
})
fromCtor
.orElse(statements.collectFirst { case ConstVal(v) => v })
.flatMap { const =>
cls.unapply(const.value)
}
def fromBody = statements.collectFirst { case ConstVal(v) => v }
fromCtor.orElse(fromBody).flatMap { const => cls.unapply(const.value) }
}
case _ =>
Option.empty[ValueType]
Expand Down
6 changes: 6 additions & 0 deletions macros/src/test/scala/enumeratum/CompilationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,9 @@ object F {
case object F4 extends F(value = 4, "mike")

}

sealed abstract class G(val name: String, val value: Int)
object G {
val values = FindValEnums[G]
case object G1 extends G("gerald", 1)
}

0 comments on commit a1cde16

Please sign in to comment.