Why don't I see results of printfn called outside the main entrypoint (F#) -


i have f# console application calls functions in other modules perform work main function entrypoint. have series of printfn in these other functions provide me information on running of program.

when compiled in debug mode, statements print console. however, when compiled in release mode statements print console directly inside main entrypoint function.

what can print statements info in these other modules?

a code example provided below:

program.fs

[<entrypoint>] let main argv =    printfn "%s" "start"   // prints in release , debug mode    file1.run    printfn "%s" "end"     // prints in release , debug mode   system.console.readline() |> ignore   0 // return integer exit code 

file1.fs

module file1  let run =    let x = 1   printfn "%d" x  // won't print in release mode 

yep (kindof) right - not print run expression here , seems compiler optimizing away in release mode.

and why should not? in perfect (pure/referential transparent) world have expression of type unit can have single value () ... , don't use or remember value!

to honest don't know if bug or feature ;)

anyway simple trick , indeed should not use expression effects in way did:

let run () =    let x = 1   printfn "%d" x  ...  file1.run () 

see - it's function , get's called @ right time , output ;)


btw: if interested in kind of stuff either use tools reflector (which not have @ hand @ moment) or use il dasm (a tool vs should install anyway) - if @ compiled debug/release assemblies notice this:

il_001f:  call       class [fsharp.core]microsoft.fsharp.core.unit file1::get_run() 

can found in release version if use expression.


i played bit , have creative make compiler stuff:

for example

let reallyneed v =     if v = ()  [<entrypoint>] let main argv =    printfn "%s" "start"   // prints in release , debug mode    file1.run |> reallyneed     printfn "%s" "end"     // prints in release , debug mode   system.console.readline () |> ignore   0 // return integer exit code 

works (it prints 1) - while

ignore file1.run 

or

let reallyneed v = ignore v 

don`t ;) - seems have actually use value somewhere :d


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -