Skip to content

[Scala 3] port ValueEnum (top-level valNameImpl)#883

Draft
halotukozak wants to merge 3 commits into
AVSystem:scala-3from
halotukozak:05-07-value-enum
Draft

[Scala 3] port ValueEnum (top-level valNameImpl)#883
halotukozak wants to merge 3 commits into
AVSystem:scala-3from
halotukozak:05-07-value-enum

Conversation

@halotukozak

Copy link
Copy Markdown
Member

Restores misc.ValueEnum family from Phase-1 ??? stub to a real Scala 3 macro implementation. Cribbed verbatim from fork origin/master:core/src/main/scala-3/com/avsystem/commons/misc/ValueEnum.scala.

What changed

ValueEnum.scala:

  • Top-level def valNameImpl[T <: ValueEnum: Type, ValName: Type, Owner: Type] — Pattern 5 enclosing-symbol walk via Symbol.spliceOwner.owner + omitAnonClass helper. Validates that the macro splice site is a public, final, non-lazy val in the companion object.
  • ValueEnumCompanion: protected[this] implicit def valName: ValName = ??? is now inline protected given ValName = $${ valNameImpl[Value, ValName, this.type]('{ ValName(_) }) }.
  • Ctx registration machinery preserved verbatim (synchronized + awaitingRegister/finished flag dance + lazy val values) — initialization order matches Scala 2 for the SI-7046-style trap.
  • AbstractValueEnum(implicit val enumCtx: EnumCtx) switched to AbstractValueEnum(using protected val enumCtx: EnumCtx).

Givens:

  • implicit final val ordering: Ordering[T] is now given ordering: Ordering[T] = Ordering.by(_.ordinal). Public name preserved.
  • implicit final def ordered(value: T): Ordered[T] modernized to given ordered: Conversion[T, Ordered[T]] = Ordered.orderingToOrdered(_). Downstream enumA < enumB keeps compiling without an extra import scala.math.Ordered.orderingToOrdered.

Tests

ValueEnumTest un-wrapped. value enum test green — validates values == List(One, Two, Three, Four, Five_?), ordinals 0..4, names match declaration. enum constant member validation stays ignored (matches fork — compile-time assertCompiles/assertDoesNotCompile of macro error contract is harder to reproduce in Scala 3 toolbox).

MIGRATION.md

  • §3 core — misc ValueEnum (slice 5.7) entry — full reshape rationale.
  • Backlog row for ValueEnumCompanion.valName removed.

Slice 5.7 / Phase 5 leaf-feature-restoration.

Verbatim port from origin/master:core/src/main/scala-3/com/avsystem/commons/misc/ValueEnum.scala.

- Top-level `def valNameImpl` (NOT in MiscMacros — Pattern 5 enclosing-symbol walk via
  `Symbol.spliceOwner.owner` + `omitAnonClass`; Pitfall 5 — `.owner` required, not bare
  `spliceOwner`).
- `Ctx` registration machinery (synchronized + `awaitingRegister` flag dance + `finished`
  flag + `lazy val values`) preserved verbatim per Pitfall 8 (SI-7046-style init-order trap).
- `given (valName: ValName) => EnumCtx = new Ctx(...)` replaces the Phase-1 `implicit def
  valName: ValName = ???` stub.
- `inline protected given ValName = ${ valNameImpl[Value, ValName, this.type]('{ ValName(_) }) }`
  wires call sites to the top-level macro.
- Rule-3 auto-fix: added local `import scala.quoted.{Expr, Quotes, Type}` because our
  `CommonAliases.scala` on this branch base lacks the fork's `export scala.quoted.*` line
  (matches slice 5.3/5.5 precedent).
- Rule-3 auto-fix: bundled new `ValueEnumCompanionCompat.scala` (single trait, no extra
  deps — only needs `summon[Ordering[T]]` which is provided by `given Ordering[T]` in
  ValueEnum.scala). Fork keeps it in `compat.scala` alongside many other compat traits;
  we extract just the ValueEnum-relevant one to avoid dragging in unported feature compat
  surface (Boxing/Opt/Timestamp/TypeString/JavaClassName/NamedEnumCompanion/OrderedEnum).
Synced verbatim from origin/master:core/src/test/scala-3/com/avsystem/commons/misc/ValueEnumTest.scala.

- "value enum test" un-wrapped + green: validates ordinals (0..4) + names
  (One/Two/Three/Four/Five_?) + values collection ordering — confirms
  Ctx synchronized + awaitingRegister flag dance produces correct results
  (no IllegalStateException at startup, Pitfall 8 cleared).
- "enum constant member validation" stays `ignore`d per fork (`assertCompiles`/
  `assertDoesNotCompile` of the macro's compile-time error contract is harder to
  reproduce in Scala 3 toolbox — fork keeps it ignored too).
@halotukozak halotukozak added this to the Scala 3 milestone Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant