Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does dollar sign do in scala

Tags:

scala

When I was reading spark source code here, I saw code like $(a_variable). What does it mean?

I copy the code here:

  final val blockSize: IntParam = new IntParam(this, "blockSize",
    "Block size for stacking input data in matrices. Data is stacked within partitions." +
      " If block size is more than remaining data in a partition then " +
      "it is adjusted to the size of this data. Recommended size is between 10 and 1000",
    ParamValidators.gt(0))

  /** @group getParam */
  final def getBlockSize: Int = $(blockSize)
like image 558
worldterminator Avatar asked Oct 01 '15 02:10

worldterminator


People also ask

What is dollar in Scala?

That isn't special Scala syntax, it's a method name. In Scala $ is a legal identifier. The method is inherited from the org. apache.

What is dollar in coding?

The dollar sign "$" has Unicode code point U+0024 (inherited from ASCII via Latin-1). There are no separate encodings for one- and two-line variants.

What is F interpolation in Scala?

The f Interpolator Prepending f to any string literal allows the creation of simple formatted strings, similar to printf in other languages. When using the f interpolator, all variable references should be followed by a printf -style format string, like %d . Let's look at an example: Scala 2 and 3.

What does string * mean in Scala?

In Scala, as in Java, a string is an immutable object, that is, an object that cannot be modified. On the other hand, objects that can be modified, like arrays, are called mutable objects. Strings are very useful objects, in the rest of this section, we present important methods of java. lang.


2 Answers

That isn't special Scala syntax, it's a method name. In Scala $ is a legal identifier. The method is inherited from the org.apache.spark.ml.param.Param trait.

See the source.

like image 51
Michael Zajac Avatar answered Oct 20 '22 22:10

Michael Zajac


I believe that the dollar sign is usually used for string interpolation. What that means is that if I want to use a val(ue)/var(iable) inside a string (denoted with an s before the first double quotation), I can access such vals/vars using the $ sign. Otherwise, we won't be able to use vals/vars inside a string since it would not know how to escape the string characters.

Example:

val favoriteFruit = "strawberry" val strawberryCount = 12  println(s"My favorite fruit is ${favoriteFruit}. I've had ${strawberryCount} pieces today!") // string interpolation 

In your case, however it seems that $ is a shortcut to getOrDefault the var/val (as sourced by @Michael Zajac and @kingledion...gets the value/variable. If it does not exist, gets the default value/variable). Using getOrDefault may be a more robust solution in cases in which you expect the parameter to not always have a corresponding value, for which you can set a default value.

like image 40
sweetmusicality Avatar answered Sep 18 '22 19:09

sweetmusicality