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

Non-literal values for ValueEnums don't work. #59

Closed
randomstatistic opened this issue Aug 8, 2016 · 3 comments
Closed

Non-literal values for ValueEnums don't work. #59

randomstatistic opened this issue Aug 8, 2016 · 3 comments

Comments

@randomstatistic
Copy link

I'm converting some of my stuff to the StringEnumEntry, (Thanks!) and I'm noticing a few things I did earlier (With stock EnumEntry) that don't work with this. It's quite possible the answers will be "don't do that".

I'd set up an intermediate, non-sealed Entry-type class to attach some common methods to. The following doesn't compile:

abstract class StringEntryWithHelpers(override val value: String) extends StringEnumEntry {
  //...
}


sealed abstract class Parameter(name: String) extends StringEntryWithHelpers(name)
object Parameters extends StringEnum[Parameter] {
  object FOO extends Parameter("foo")

  override def values = findValues
}

@randomstatistic
Copy link
Author

I suppose it's worth noting that it's not the intermediate class so much as the intermediate constructor that's doing it. This works, for example:

abstract class StringEntryWithHelpers extends StringEnumEntry {
  //...
}


sealed abstract class Parameter(val value: String) extends StringEntryWithHelpers
object Parameters extends StringEnum[Parameter] {
  object FOO extends Parameter("foo")

  override def values = findValues
}

I'd hoped to hide the "val value" requirement was all.

@lloydmeta
Copy link
Owner

lloydmeta commented Aug 9, 2016

Unfortunately, there is currently a strict requirement to have the value member of all of these value enums be implemented as a literal, in order for us to do the uniqueness check. There might be a way to sling enough macro magic to support what you're proposing (look up the class hierarchy to resolve that value is being implemented by a name member that is supplied as a literal at instantiation), but it seems like it'll be very difficult, and perhaps error-prone due to edge cases.

@randomstatistic
Copy link
Author

Alright, I'm using the phrasing in my second comment, I was just a little concerned that the "val value" was a bit of magic future maintainers might not notice. It's hard to decide between

sealed abstract class Parameter(val value: String) extends StringEnumEntry
// with
object FOO extends Parameter("foo")

and

sealed abstract class Parameter extends StringEnumEntry
// with
object FOO extends Parameter { val value = "foo" }

Not a huge deal either way though.

@lloydmeta lloydmeta changed the title Having an intermediate class doesn't work Non-literal values for ValueEnums don't work. Aug 12, 2016
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

No branches or pull requests

2 participants