In the following program,
module Program
let condition = System.DateTime.Now.Millisecond % 2 = 0
let inline reliesOnCondition (x:int) =
if condition then
printfn "%i" x
[<EntryPoint>]
let main args =
reliesOnConditional System.DateTime.Now.Second
0
will the JIT optimize away the expression reliesOnCondition System.DateTime.Now.Second if condition turns out to be false upon module loading?
The upvoted answer is incorrect, a property accessor indirection does not in general prevent the JIT optimizer from omitting dead code. Most simple ones get inlined, their compile time values are considered. Which means that it will not optimize away the expression since the value of condition is only known at run time. The fact that the code is jitted after the condition value is already known doesn’t change this, the optimizer must be able to determine the value statically. Only a literal will suffice here, you’d get one with conditional compilation (#if in C#). Check this answer for more background info on the optimizer.