继续探讨 F# workflow Buildingers, I 决定对建筑商进行例外处理,特别是在“for.in.do”建筑中。
这一试验的终极目标是工作流程建设者,即使在出现例外情况时,也将继续处理顺序。
The below, for example, doesn t work, because the For method of the builder does not appear to get called until after the point where the exception occurs (at the first Directory.EnumerateFiles(dir) call)
type failSafeSeq() =
member this.For(seq1, mapFunction) =
try
seq1 |> Seq.collect mapFunction
with
ex -> Console.WriteLine ex
Seq.empty
member this.Yield(yieldExpr) = yieldExpr |> Seq.singleton
member this.YieldFrom(yieldBang) = yieldBang
member this.Combine(a, b) = Seq.append a b
member this.Delay(delayFun) = delayFun()
member this.Zero() = Seq.empty
let failSafe = new failSafeSeq();
let rec allFilesSeq dir =
failSafe { for file in Directory.EnumerateFiles(dir) do yield file
for subdir in Directory.EnumerateDirectories dir do yield! (allFilesSeq subdir) }
[<EntryPoint>]
let main args =
allFilesSeq "C:\System Volume Information\" //almost guaranteed to cause an UnauthorizedAccessException on Windows systems at the first Directory.EnumerateFiles(dir) call.
|> Seq.iter Console.WriteLine
0
利用F#工作流程是可能的?