What the heck is the following type:
(Int, => Double) => String
Note the trailing comma after Int
. Apparently it is not a syntactic loophole, but something different from
(Int => Double) => String
E.g. when using overloading:
trait Foo {
def bar(x: (Int, => Double) => String): Unit
def bar(x: (Int => Double) => String): Unit
}
Trailing commas (sometimes called "final commas") can be useful when adding new elements, parameters, or properties to JavaScript code. If you want to add a new property, you can add a new line without modifying the previously last line if that line already uses a trailing comma.
Description: In Python, a tuple is actually created by the comma symbol, not by the parentheses. Unfortunately, one can actually create a tuple by misplacing a trailing comma, which can lead to potential weird bugs in your code. You should always use parentheses explicitly for creating a tuple.
To remove the leading and trailing comma from a string, call the replace() method with the following regular expression as the first parameter - /(^,)|(,$)/g and an empty string as the second.
It's a common syntactical convention to allow trailing commas in an array, languages like C and Java allow it, and Python seems to have adopted this convention for its list data structure.
(Int, => Double) => String
is a function with by-name second argument (=> Double
).
You can't create a Function2[Int, => Double, String]
, but you could create a lambda (Int, => Double) => String
, that means the same:
scala> def s:(Int, => Double) => String =
| (a, b) => if (a > 0) a.toString else b.toString
s: (Int, => Double) => String
scala> s(1, {println("test"); 2.0}) //second parameter is not evaluated
res0: String = 1
scala> s(-1, {println("test"); 2.0})
test
res1: String = 2.0
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