Reputation: 7830
I have written this F# console application (that should call the run
method only if the condition is met - if the directory exists):
open System
let dump (x: string) =
Console.WriteLine(x)
let validateDirectory directory funToCallWhenOk =
match (System.IO.Directory.Exists directory) with
| true -> funToCallWhenOk
| false -> dump (sprintf "Problem with '%s'" directory)
//if System.IO.Directory.Exists directory then
// funToCallWhenOk
//else
// dump (sprintf "Problem with '%s'" directory)
let Watch directory =
let run =
dump (sprintf "Now watching '%s'" directory)
dump "Press 'q' to quit"
validateDirectory directory run
[<EntryPoint>]
let main argv =
Watch @"c:\temp\resize1"
0
The currently specified directory does not exist, but the run
function is called anyway, regardles if using match .. when
or if then else
. The console output:
Why is that?
Upvotes: 2
Views: 57
Reputation: 5805
You haven't defined run
as a function but as a unit
value. You must define it as a function and call the function in validateDirectory
:
let validateDirectory directory funToCallWhenOk =
match (System.IO.Directory.Exists directory) with
| true -> funToCallWhenOk()
| false -> dump (sprintf "Problem with '%s'" directory)
//if System.IO.Directory.Exists directory then
// funToCallWhenOk
//else
// dump (sprintf "Problem with '%s'" directory)
let Watch directory =
let run() =
dump (sprintf "Now watching '%s'" directory)
dump "Press 'q' to quit"
validateDirectory directory run
Upvotes: 3