Skip to content

Commit

Permalink
Try ordinals
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydmeta committed Apr 21, 2015
1 parent 074c4ec commit 48db104
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
6 changes: 3 additions & 3 deletions enumeratum-core/src/main/scala/enumeratum/Enum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ trait Enum[A <: EnumEntry] {
* Feel free to implement this however you'd like (including ordering, etc) if that
* fits your needs better.
*/
def values: Iterable[A]
def values: Seq[A]

/**
* Method that returns the Set of [[A]] objects that the macro was able to find.
* Method that returns an IndexedSeq of [[A]] objects that the macro was able to find.
*
* You will want to use this in some way to implement your [[values]] method. In fact,
* if you aren't using this method...why are you even bothering with this lib?
*/
protected def findValues: Set[A] = macro EnumMacros.findValuesImpl[A]
protected def findValues: IndexedSeq[A] = macro EnumMacros.findValuesImpl[A]

/**
* Map of [[A]] object names to [[A]]s
Expand Down
12 changes: 6 additions & 6 deletions enumeratum-core/src/test/scala/enumeratum/EnumSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class EnumSpec extends FunSpec with Matchers {
describe("#values") {

it("should contain objects") {
DummyEnum.values should be(Set(Hello, GoodBye, Hi))
DummyEnum.values should be(IndexedSeq(Hello, GoodBye, Hi))
}

}
Expand Down Expand Up @@ -75,7 +75,7 @@ class EnumSpec extends FunSpec with Matchers {
describe("#values") {

it("should contain objects") {
SmartEnum.values should be(Set(Hello, GoodBye, Hi))
SmartEnum.values should be(IndexedSeq(Hello, GoodBye, Hi))
}

}
Expand Down Expand Up @@ -105,17 +105,17 @@ class EnumSpec extends FunSpec with Matchers {
describe("#values") {

it("should contain objects") {
values shouldBe Set(FlyAgaric, LSD, Shimeji)
values shouldBe IndexedSeq(FlyAgaric, LSD, Shimeji)
}

}

describe("#withName") {

it("should return the proper object when passed the proper string") {
withName("FlyAgaric") should be(FlyAgaric)
withName("LSD") should be(LSD)
withName("Shimeji") should be(Shimeji)
withName("FlyAgaric") shouldBe FlyAgaric
withName("LSD") shouldBe LSD
withName("Shimeji") shouldBe Shimeji
}

it("should throw an error otherwise") {
Expand Down
10 changes: 6 additions & 4 deletions macros/src/main/scala/enumeratum/EnumMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import scala.util.control.NonFatal

object EnumMacros {

def findValuesImpl[A: c.WeakTypeTag](c: Context): c.Expr[Set[A]] = {
def findValuesImpl[A: c.WeakTypeTag](c: Context): c.Expr[IndexedSeq[A]] = {
import c.universe._
val resultType = implicitly[c.WeakTypeTag[A]].tpe
val typeSymbol = weakTypeOf[A].typeSymbol
validateType(c)(typeSymbol)
val subclassSymbols = enclosedSubClasses(c)(typeSymbol)
c.Expr[Set[A]](q"Set[${tq"$resultType"}](..${subclassSymbols.map(s => Ident(s))})")
c.Expr[IndexedSeq[A]](q"IndexedSeq[${tq"$resultType"}](..${subclassSymbols.map(s => Ident(s))})")
}

private[this] def validateType(c: Context)(typeSymbol: c.universe.Symbol): Unit = {
Expand All @@ -39,8 +39,10 @@ object EnumMacros {
*/
val enclosingModule = c.enclosingClass match {
case md @ ModuleDef(_, _, _) => md
case _ => c.abort(c.enclosingPosition,
"The enum (i.e. the class containing the case objects and the call to `findValues`) must be an object")
case _ => c.abort(
c.enclosingPosition,
"The enum (i.e. the class containing the case objects and the call to `findValues`) must be an object"
)
}
enclosingModule.impl.body.filter { x =>
try {
Expand Down

0 comments on commit 48db104

Please sign in to comment.