Unit in scala

Usually Unit is only used to declare the return value of a function or method:

 def output1(in:Int):Unit = {
      println(in)
    }
output1(2)
 def output(fn:(Int) => Unit) = {
      fn(2)
    }
    output(println)

output: 2

In addition, if the compiler judges that the result returned is not of Unit type, it will automatically return () at the end

def output1(in:Int):Unit = {
      println(in)
    }
    val rs = output1(2)
    println(rs)

Output result: ()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324965986&siteId=291194637