Print values ​​returned by scala

object TestClass { def main (args: Array[String]) { println("Hello World"); val c = List (1,2,3,4,5,6,7,8,9,10) println(findMax(c)) } def findMax (tempratures: List[Int]) { tempratures.foldLeft(Integer.MIN_VALUE) {Math.max} } } 

Shown result:

 Hello World () 

Why is the conclusion not

 Hello World 10 

I do it in IntelliJ

+4
source share
2 answers

This is one of the most common typos scala.

You are missing = at the end of your method:

 def findMax (tempratures: List[Int]) { 

Must read:

 def findMax (tempratures: List[Int]) = { 

Exiting = off means your method returns Unit (nothing).

+10
source

Because you define findMax without a return type, so the return type is Unit or () .

 def findMax (tempratures: List[Int]) { ... } 

aka

 def findMax (tempratures: List[Int]) : Unit = { ... } 

Instead you want

 def findMax (tempratures: List[Int]) : Int = { ... } 

or omitted type

 def findMax (tempratures: List[Int]) = { ... } 
+7
source

All Articles