Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does $( ) mean in Scala?

I encounter this when I am reading code from apache-spark:

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

I have searched for a long time in the web but still cannot find good explanation of what does $(alpha) mean here? How can it assign a self-defined class DoubleParam variable to Double? Thanks!!

FYI, The DoubleParam class is defined as:

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)
}
like image 281
PunyTitan Avatar asked Aug 13 '15 09:08

PunyTitan


People also ask

What is .type in Scala?

Type declaration is a Scala feature that enables us to declare our own types. In this short tutorial, we'll learn how to do type declaration in Scala using the type keyword. First, we'll learn to use it as a type alias. Then, we'll learn to declare an abstract type member and implement it.

What does () mean in Scala?

It can denote a function or method that takes no parameters, such as: def foo() = "Hello world" Note that when writing an anonymous function, the () is by itself but still means a function with no parameters. val x = () => 2.

What does ::: mean in Scala?

:: - adds an element at the beginning of a list and returns a list with the added element. ::: - concatenates two lists and returns the concatenated list.

What's * mean in Scala?

: _* is a special instance of type ascription which tells the compiler to treat a single argument of a sequence type as a variable argument sequence, i.e. varargs.


1 Answers

$() is a Spark function defined in the trait Params. It simply calls getOrDefault on the Params object.

It's definition is

/** An alias for [[getOrDefault()]]. */
protected final def $[T](param: Param[T]): T = getOrDefault(param)
like image 76
Till Rohrmann Avatar answered Sep 20 '22 03:09

Till Rohrmann