Skip to content

Instantly share code, notes, and snippets.

@stephanos
Last active December 12, 2015 04:49
Show Gist options
  • Save stephanos/4717443 to your computer and use it in GitHub Desktop.
Save stephanos/4717443 to your computer and use it in GitHub Desktop.
import play.api.libs.json.Json
case class MyClass(id: Int, text: String)
object MyClass {
implicit val mailRead = Json.reads[MyClass]
def apply(id: Int) =
apply(id, "")
}
[error] exception during macro expansion:
[error] scala.ScalaReflectionException: value apply encapsulates multiple overloaded alternatives and cannot be treated as a method. Consider invoking `<offending symbol>.asTerm.alternatives` and manually picking the required method
[error] at scala.reflect.api.Symbols$SymbolApi$class.asMethod(Symbols.scala:279)
[error] at scala.reflect.internal.Symbols$SymbolContextApiImpl.asMethod(Symbols.scala:73)
[error] at play.api.libs.json.JsMacroImpl$.readsImpl(JsMacroImpl.scala:44)
[error] one error found
@mandubian
Copy link

Hi,

You should have a mix of 2 issues here apparently if you use Play2.1-RC2

1st one

First if you want to put your implicit macro in companion, you should write it like

object MyClass extends Function2[Int, String, MyClass] {

implicit val mailRead = Json.reads[MyClass]
}

It's explained in this ticket https://play.lighthouseapp.com/projects/82401/tickets/932-new-json-api-and-companion-objects

This issue has been corrected in Play2.1-master (but won't be in 2.1.0 final)

2nd one

You shouldn't override apply function in companion object because Scala macro can't decide which one it should choose.
There is not workaround for this one but to call your function with another name that apply like

...
 def fromId(id: Int) = apply(id, "")
...

MyClass.fromId(123)

@stephanos
Copy link
Author

Thank you for the answer! By the way, the JSON API rulez!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment