【问题标题】:ScalaJS enumerationScalaJS 枚举
【发布时间】:2015-06-30 08:32:29
【问题描述】:

我试图为AngularJS-Toaster 使用枚举

class Toaster extends js.Object{
  // see https://github.com/jirikavi/AngularJS-    Toaster/blob/master/toaster.js#L58
  def pop(`type` : ToasterType,title : String,body:String) : Unit = js.native
}

这样的枚举

object ToasterType extends Enumeration{
  type ToasterType = Value
  val Success = Value("success")
  ...

}

然后像这样调用

toaster.pop(ToasterType.Success, "some title", "text")

当烤面包机弹出时,没有标题或文本。当我在pop 定义上使用字符串类型并传递“成功”时,一切都按预期工作。

(我使用了http://www.scala-js.org/doc/semantics.htmlhttps://github.com/scala-js/scala-js/issues/38。认为上面应该可以工作,不是吗?)

【问题讨论】:

    标签: scala scala.js


    【解决方案1】:

    Scala Enumerations 不是 Strings,所以这应该行不通。您必须使用 String,这是唯一的 JavaScript string

    【讨论】:

    • Scala.js 没有魔法。你可以做的是隐式皮条客pop的变体,它接受你的枚举,并调用原始的pop方法,将tpe.toString()传递给type参数。您不需要语言支持或宏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 2015-08-08
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多