0

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?

Sam
  • 2,745
  • 3
  • 20
  • 42
  • Why do you care about this - you can use an IL disasembler to find out anyway? – John Palmer Jun 26 '15 at 09:51
  • As a hint, change `let b...` to `let b = 2000 / 0` and see what happens ;) – Tim Rogers Jun 26 '15 at 09:55
  • @TimRogers thanks. I hadn't seen the connection between what I was asking and `lazy` computation till you mentioned it. – Sam Jun 26 '15 at 09:59
  • @JohnPalmer I am trying to generate/compile code via F# quotation expressions and want to understand the runtime behaviour of the resulting code given that there is potentially significant branching required (making large portions of code redundant). – Sam Jun 26 '15 at 10:01

0 Answers0