James Cowhen
James Cowhen

Reputation: 2955

Play2 Implicit Writes<T> in Java

I have some problem calling my Scala code from Java.

Here is my Scala Class:

case class Foobar(foo: String) extends FoobarParent

object Foobar {

    implicit object Format extends Format[Foobar] {
        def writes(Foobar: foobar): JsValue = {       
           ....
        }

        implicit def reads(json: JsValue): JsResult[Foobar] = {
            ...
        }
    }
}

Now when I have a method with the following signature:

def publish[T <: FoobarParent](foobarParent: T)(implicit writes: Writes[T]): Unit = {...}

This works fine when calling from Scala code, I simply just do publish[Foobar] (Foobar(...))

However in Java, the signature looks likes this in my IDE:

publish (T FoobarParent, Writes<T> writes)

Now my question is what/how do I fulfil those two parameters in Java?

Upvotes: 0

Views: 93

Answers (1)

kapex
kapex

Reputation: 29969

You usually can get an object's instance like this: Foobar$.MODULE$
and the nested one like this: Foobar.Format$.MODULE$

There's a problem with the companion object here though, because it gets compiled as a different class. It will create a class named Foobar$ which is not of the type Foobar nor does it extends FoobarParent. So you can't just call publish(Foobar$.MODULE$, Foobar.Format$.MODULE$);. I think you'll just have to create a new instance:

publish(new Foobar("..."), Foobar.Format$.MODULE$);

Upvotes: 1

Related Questions