From 26673375d7e431cc4dbb1f16f77a998edea97ef7 Mon Sep 17 00:00:00 2001 From: Georgi Krastev Date: Mon, 12 Sep 2022 21:59:44 +0200 Subject: [PATCH] Make enum example serializable --- examples/src/main/scala/shapeless/examples/enum.scala | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/src/main/scala/shapeless/examples/enum.scala b/examples/src/main/scala/shapeless/examples/enum.scala index a7f39bfcb..c594b724c 100644 --- a/examples/src/main/scala/shapeless/examples/enum.scala +++ b/examples/src/main/scala/shapeless/examples/enum.scala @@ -49,9 +49,12 @@ object ScalaEnumDemo /*extends App*/ { // object ShapelessEnumDemo extends App { // ADT as an enumeration. Barely any more boilerplate ... - sealed trait WeekDay + sealed abstract class WeekDay(val ordinal: Int) extends Serializable object WeekDay { - val Mon, Tue, Wed, Thu, Fri, Sat, Sun = new WeekDay {} + private var ordinal = 0 + val Mon, Tue, Wed, Thu, Fri, Sat, Sun = + try new WeekDay(ordinal) {} + finally ordinal += 1 val values: Set[WeekDay] = Values } @@ -68,7 +71,8 @@ object ShapelessEnumDemo extends App { case _ => false // compile time non-exhaustive match warning/error without this case } - assert(!isWeekend(Mon)) // + assert(!isWeekend(Mon)) + assert(values.size == values.map(_.ordinal).size) } // Infrastructure for the above. Original version due to Travis Brown,