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

Any way to get rid of the val values = findValues boilerplate? #141

Open
i-am-the-slime opened this issue May 17, 2017 · 5 comments
Open

Comments

@i-am-the-slime
Copy link

Hey there,

Nice library!
Wouldn't it be possible to eliminate the need for
val values = findValues by simply extending EnumEntry?
Why is this needed?

@lloydmeta
Copy link
Owner

Hey,

Thanks for the feedback :)

I'm afraid I don't quite understand how extending EnumEntry would help get rid of that piece of boilerplate. Can you show me an example of what you had in mind?

@bertderbecker
Copy link

bertderbecker commented Jul 7, 2017

Hey,
I have written a little macro annotation with scalameta:

class AutoFindValues extends StaticAnnotation {

  inline def apply(defn: Any): Any = meta {
    defn match {
      case obj @ Defn.Object(mods, name, templ) =>
        obj.copy(templ = templ.copy(stats = templ.stats.map(_ :+ q"val values = findValues;")))
      case _ =>
        println(defn.structure)
        abort("@AutoFindValues must annotate an object.")
    }
  }
}

Does it work for you?

@i-am-the-slime
Copy link
Author

Hmm, seems like I understood this wrong. I thought there could just be a default implementation in EnumEntry which is lazy val values = findValues.

@i-am-the-slime
Copy link
Author

@bertderbecker Looks good to me.

@lloydmeta
Copy link
Owner

@i-am-the-slime ah yeah, unfortunately we can't put findValues as a lazy val on the base class because the macro expansion gets invoked, at which point the macro doesn't know what the implementing superclass will be. If I remember correctly, I've experimented with this in the past and it seems to work if I make it a def values = findValues instead, but that means a new list gets constructed on every call, which seems wasteful.

@bertderbecker that's a neat trick :)

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

3 participants