【问题标题】:Applications does not take parameters with scala.js or type应用程序不接受带有 scala.js 或类型的参数
【发布时间】:2017-10-10 14:26:56
【问题描述】:

我目前正在 scala-d3-js 上开发 DSL,我正在尝试使用 currying 和类型定义来避免在以下代码中出现多个映射:

trait HistogramInterface {

  import constants.Distributions._
  type Fun = ((Double) => js.Function0[Double]) | ((Double) => ((Double) => js.Function0[Double]))
  val DistributionMap: Map[String, Fun] = Map(
    Bates -> ((count: Double) => d3.random.bates(count)),
    Normal -> ((mean: Double) => (deviation: Double) => d3.random.normal(mean, deviation)),
    LogNormal -> ((mean: Double) => (deviation: Double) => d3.random.logNormal(mean, deviation)),
    IrwinHall -> ((count: Double) => d3.random.irwinHall(count))
  )


  implicit object Histogram extends HistogramInterface {
    def apply(nbBars: Int, dataArg: Seq[Double]): Selection[EventTarget] = createHistogram(nbBars, dataArg)
  }

  implicit object DistributionHistogram extends HistogramInterface {
    def apply(nbBars: Int, token: String, args: Double*): Selection[EventTarget] = {
      val DistributionFun = DistributionMap(token)(args(0)) // application does not take parameters
      val values = token match {
        case Bates => d3.range(1000).map(_ => DistributionFun.apply())
        case Normal => d3.range(1000).map(_ => DistributionFun.apply())
      }
      // Do stuff
    }
  }
}

使用此代码,我在分配常量 DistributionFun 时收到“应用程序不接受参数”。

【问题讨论】:

    标签: scala d3.js scala.js


    【解决方案1】:

    查看您的类型,DistributionMap(token)Fun,您将其定义为 or 类型:

      type Fun =
        ((Double) => js.Function0[Double]) |
        ((Double) => ((Double) => js.Function0[Double]))
    

    我认为您不能以这种方式将联合类型视为函数类型。即使可以,结果类型也会是 js.Function0[Double]) | ((Double) => js.Function0[Double])),这意味着您将无法像之后那样在不带参数的情况下调用 .apply()

    【讨论】:

    • 谢谢,这正是我所担心的。我将定义另一个 Map 来处理具有两个参数函数的情况。
    猜你喜欢
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    相关资源
    最近更新 更多