play-json-alone icon indicating copy to clipboard operation
play-json-alone copied to clipboard

case class companion object and apply doesn't work

Open hgavert opened this issue 11 years ago • 3 comments

Hi!

I was able to find working version of this, "com.typesafe.play" %% "play-json" % "2.2.0-RC2", but I stumbled across a problem. This doesn't seem to compile:

import play.api.libs.json._ import play.api.libs.functional.syntax._

case class Person(name: String, age: Int) object Person { def apply(p: String) = new Person(p, 0) implicit val personReads = Json.reads[Person] }

Yet, according to this: http://play.lighthouseapp.com/projects/82401/tickets/932-new-json-api-and-companion-objects it should have been fixed. Am I doing something wrong? I really think I need the apply method in the companion object.

hgavert avatar Sep 18 '13 20:09 hgavert

No I think this is normal... You override "apply" function and the macro is enable to detect that. As soon as you do that, you must write a custom Reads[T]

regards Pascal

On Wed, Sep 18, 2013 at 10:58 PM, hgavert [email protected] wrote:

Hi!

I was able to find working version of this, "com.typesafe.play" %% "play-json" % "2.2.0-RC2", but I stumbled across a problem. This doesn't seem to compile:

import play.api.libs.json._ import play.api.libs.functional.syntax._

case class Person(name: String, age: Int) object Person { def apply(p: String) = new Person(p, 0) implicit val personReads = Json.reads[Person] }

Yet, according to this: http://play.lighthouseapp.com/projects/82401/tickets/932-new-json-api-and-companion-objectsit should have been fixed. Am I doing something wrong? I really think I need the apply method in the companion object.

— Reply to this email directly or view it on GitHubhttps://github.com/mandubian/play-json-alone/issues/6 .

mandubian avatar Sep 18 '13 21:09 mandubian

I thought there was a way to fix that? That results in large boilerplate code to be maintained. Don't really want that.

hgavert avatar Sep 19 '13 05:09 hgavert

It's hard to fix it because we would have to decide which apply you use. So some people would be happy about the choice and others wouldn't be.

For example, in your case, what apply should be used by the macro?

The best solution IMHO is to name your custom apply with another name: For ex, def default(p: String) = Person(p, 0)

On Thu, Sep 19, 2013 at 7:39 AM, hgavert [email protected] wrote:

I thought there was a way to fix that? That results in large boilerplate code to be maintained. Don't really want that.

— Reply to this email directly or view it on GitHubhttps://github.com/mandubian/play-json-alone/issues/6#issuecomment-24718184 .

mandubian avatar Sep 19 '13 06:09 mandubian