Try to pass a function to a value but the function run

by wang kai   Last Updated June 12, 2019 07:26 AM

I have this code:

scala:

object C extends App{
 def a(): Unit ={
println("a")
}

val b=a
}

f#:

let a=printfn "%A" "a"

[<EntryPoint>]
let main argv = 
  let b=a
  0 // return an integer exit code

I only want to pass the function to a value a,not to run it,why when I run the program,got console write "a"?Thanks

Tags : scala f#


Answers 1


b is calculated when that line is executed.

try using lazy instead. this will calculate the value of b only when you call it.

object C extends App{
 def a(): Unit ={
  println("a")
 }

 lazy val b=a

}

Dionysis Nt.
Dionysis Nt.
June 12, 2019 07:24 AM

Related Questions


Updated July 20, 2017 23:26 PM

Updated April 14, 2019 13:26 PM

Updated July 16, 2018 18:26 PM

Updated May 27, 2018 13:26 PM

Updated June 27, 2018 19:26 PM