Interop between js and scala functions in Scala.js

懵懂的女人 提交于 2019-12-23 15:51:46

问题


I am trying to write a typed façade for my library Paths.js, following the official guide.

Following advice from Sebastien in Typed façade for JS library in Scala.js, I was able to flesh out part of the API. What I am now missing is a way to deal with a conversion function which is exposed in the API. Basically, the library lets you write something like

var pie = Pie({
  data: [
    { name: 'Italy', population: 59859996 },
    { name: 'Mexico', population: 118395054 },
    { name: 'France', population: 65806000 }
  ],
  accessor: function(x) { return x.population; },
  center: [20, 15],
  r: 30,
  R: 50
});

The idea is that - to simplify client code - the library does not require you to transform your data to a form that is amenable to plotting. Rather, you can provide a list of data in the form that you see fit, and then pass an accessor function to extract a number from the raw datum. This also makes it easier to associate path objects with the original datum.

The signature that I would like to expose on the Scala side would look like this:

object Pie {
  type Point = (Double, Double)
  def apply(data: Seq[A],
    accessor: A => Double,
    center: Point, r: Double, R: Double)
}

I am trying to convert accessor, which has type A => Double to something like js.Function1[js.Any, Double], and my attempt looks like (using the implicit conversion between Function1 and js.Function1)

val f: js.Any => Double = x => x match {
  case _: A => accessor(x)
  case _ => ???
}

This gives me a warning abstract type pattern A is unchecked since it is eliminated by erasure.

What would be a better way to translate this API?


回答1:


To resume the discussion in the comments:

Using a js.Function1[A, Double] is perfectly valid and reasonable in this case.

As @sjrd cites from the Scala.js doc:

JS traits and their methods can have type parameters, abstract type members and type aliases, without restriction compared to Scala's type system.



来源:https://stackoverflow.com/questions/28581329/interop-between-js-and-scala-functions-in-scala-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!