Use Round brackets...ie(), when the Type annotation is used for an argument

  1. scala> //#m:74
  2.  
  3. scala> val data = List(1, 2, 3, 4)
  4. data: List[Int] = List(1, 2, 3, 4)
  5.  
  6. scala> data.foreach(x => println(s"This is ${x}"))
  7. This is 1
  8. This is 2
  9. This is 3
  10. This is 4
  11.  
  12. scala> //This do not work, as we have to wrap x:Int with Round Brackets
  13.  
  14. scala> data.foreach(x:Int => println(s"This is ${x}"))
  15. :1: error: ')' expected but '(' found.
  16. data.foreach(x:Int => println(s"This is ${x}"))
  17. ^
  18. :1: error: ';' expected but ')' found.
  19. data.foreach(x:Int => println(s"This is ${x}"))
  20. ^
  21. scala> //When the type of argument is explicitly specified(as here x:Int),
  22. scala> //use Round brackets...ie()
  23. scala> data.foreach((x:Int) => println(s"This is ${x}"))
  24. This is 1
  25. This is 2
  26. This is 3
  27. This is 4