I have put together a basic command line application to understand the workings of the .NET runtime better. In particular, I want to know whether certain code that is declared prior to a branching statement will actually run if the subsequent branching statement renders it unnecessary for the program's output?
My F# code for a simple Console Application (with comments) below:
[<EntryPoint>]
let main argv =
// I have given the input "2" at the command line
let (status, result) = System.Int32.TryParse argv.[0]
let a = 1
let b = 2000 * 5 + 9 // Will this ever be evaluated at runtime ??
let fa x = x + 1
let fb x = x + 200 // In a similar vein, will this need to be declared at RUNTIME
let answer =
if result > 1 then
fa a // the path it will take at RUNTIME
else
fb b // the path it will NOT take at RUNTIME
printfn "%d" answer
System.Console.ReadLine() |> ignore
0 // return an integer exit code
In summary: b
and fb
will certainly be compiled into the executable BUT will they actually be evaluated for the purposes of this running program or will the runtime optimize them away at runtime?