0

情况:一个函数返回一个 Option(Stream)。在调用函数中,我必须以某种方式打开它,以便在使用后处理流。我应该把“使用”声明放在哪里?

let getSteamOpt = if System.Random().NextDouble() > 0.5 then Some(new MemoryStream()) else None
let createAndUseStreamIfAny = 
    let streamOpt = getSteamOpt
    match streamOpt with
    | Some( (* use <-- doesn't work like this *) stream) -> printf "stream has to be disposed after this"
    | None -> printf "blah"
4

1 回答 1

3
let createAndUseStreamIfAny = 
    let streamOpt = getSteamOpt
    match streamOpt with
    | Some(s) -> 
        use stream = s
        printf "stream has to be disposed after this"
    | None -> printf "blah"
于 2013-09-08T16:09:33.880 回答