- scala> //#m:74
- scala> val data = List(1, 2, 3, 4)
- data: List[Int] = List(1, 2, 3, 4)
- scala> data.foreach(x => println(s"This is ${x}"))
- This is 1
- This is 2
- This is 3
- This is 4
- scala> //This do not work, as we have to wrap x:Int with Round Brackets
- scala> data.foreach(x:Int => println(s"This is ${x}"))
:1: error: ')' expected but '(' found. data.foreach(x:Int => println(s"This is ${x}")) ^ :1: error: ';' expected but ')' found. data.foreach(x:Int => println(s"This is ${x}")) ^ scala> //When the type of argument is explicitly specified(as here x:Int), scala> //use Round brackets...ie() scala> data.foreach((x:Int) => println(s"This is ${x}")) This is 1 This is 2 This is 3 This is 4
Use Round brackets...ie(), when the Type annotation is used for an argument
Subscribe to:
Posts (Atom)