0

This question is a question related with How to export properties of shared case classes

In case I have a case class of the form

case class Foo(var id : Long,var title: Seq[String])

i.e. it contains a sequence of data, is there any setting to be exported as js.Array ?

Community
  • 1
  • 1

2 Answers2

1

I use in general upickle, the way I went around is by using generics in the shared project i.e.

case class Foo[S[_]](var id : Long, var title: S[String])

in scala JVM I pickle the case class as an object of the form

Foo(id, Seq(title1, title2, ...))

which it is an object of type Foo[Seq] and in scala JS I pickle the object as Foo[js.Array] .

0

In a Scala.js-only project, one would simply do the following:

case class Foo(var id: Long, var title: Seq[String]) {
  @JSExport("title")
  protected def jsTitle: js.Array[String] =
    title.toJSArray

  @JSExport("title")
  protected def jsTitle_=(v: js.Array[string]): Unit =
    title = v.toSeq
}

however, this will refuse to compile in shared sources, because Scala/JVM does not know about js.Array.

I'm afraid there is no boilerplate free solution to your problem. The easiest solution would be to declare Foo separately in the JVM and JS parts of your project. You can then still use it from the shared sources.

sjrd
  • 21,805
  • 2
  • 61
  • 91