Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trailing comma in a type

Tags:

types

scala

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
}
like image 542
0__ Avatar asked Jan 01 '14 12:01

0__


People also ask

Should you use trailing commas?

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.

What is a trailing comma Python?

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.

How do I remove a trailing comma in typescript?

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.

Does Python allow trailing commas?

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.


1 Answers

(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
like image 70
senia Avatar answered Oct 21 '22 13:10

senia