scalajs-react-components icon indicating copy to clipboard operation
scalajs-react-components copied to clipboard

JSMacro should support union types

Open mariussoutier opened this issue 9 years ago • 6 comments

When using union types, one must cast to js.Any, e.g.:

case class Pie(padding: js.UndefOr[Double | Shape] = js.undefined) {
  def toJs = {
    val p = js.Dynamic.literal()
    padding.foreach(x => p.updateDynamic("padding")(x.asInstanceOf[js.Any]))
    p
  }
}

JSMacro doesn't know about this, so this compiler error comes up:

type mismatch;
 found   : scala.scalajs.js.|[Double,Shape]
 required: scala.scalajs.js.Any

mariussoutier avatar Feb 26 '16 10:02 mariussoutier

hey you need this import

import chandu0101.scalajs.react.components._

https://github.com/chandu0101/scalajs-react-components/blob/master/core/src/main/scala/chandu0101/scalajs/react/components/package.scala#L16

chandu0101 avatar Feb 26 '16 10:02 chandu0101

Ah thanks.

The import is package private, so I can't use it for my own stuff. I copied the code, but it produces strange errors in Play that it can't find its config files...

mariussoutier avatar Feb 26 '16 13:02 mariussoutier

I'm not sure what to do about this. The implicit that we use is not safe in the general case, which is why we no longer export it (see https://github.com/scala-js/scala-js/pull/2070 for details), but you can easily provide your own. Or cast yourself, like you figured out.

Would @sjrd have any idea, perhaps?

oyvindberg avatar Feb 26 '16 13:02 oyvindberg

What do you mean, it's not safe?

mariussoutier avatar Feb 26 '16 13:02 mariussoutier

safe as in, you can get runtime exceptions

oyvindberg avatar Feb 26 '16 13:02 oyvindberg

Thanks. Then I'll use my internal version for now.

mariussoutier avatar Feb 26 '16 13:02 mariussoutier