在Scala中$()的含义是什么?

时间:2015-08-13 09:36:54

标签: scala apache-spark

当我从apache-spark中读取代码时,我遇到了这个问题:

    val alpha = new DoubleParam(this, "alpha", "alpha for implicit preference",
                                ParamValidators.gtEq(0)) 
    /** @group getParam */
    def getAlpha: Double = $(alpha)

我在网上搜索了很长时间但仍无法找到$(alpha)在这里的含义的好解释?如何将自定义的类DoubleParam变量分配给Double?谢谢!

仅供参考,DoubleParam类定义为:

class DoubleParam(parent: String, name: String, doc: String, isValid: Double => Boolean)
  extends Param[Double](parent, name, doc, isValid) {

  def this(parent: String, name: String, doc: String) =
    this(parent, name, doc, ParamValidators.alwaysTrue)

  def this(parent: Identifiable, name: String, doc: String, isValid: Double => Boolean) =
    this(parent.uid, name, doc, isValid)

  def this(parent: Identifiable, name: String, doc: String) = this(parent.uid, name, doc)

  /** Creates a param pair with the given value (for Java). */
  override def w(value: Double): ParamPair[Double] = super.w(value)
}

1 个答案:

答案 0 :(得分:8)

$()是特征Params中定义的Spark函数。它只是在getOrDefault对象上调用Params

它的定义是

/** An alias for [[getOrDefault()]]. */
protected final def $[T](param: Param[T]): T = getOrDefault(param)