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)
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With