构造值初始化

时间:2013-12-17 05:48:43

标签: f#

我从F#开始,我无法理解构造值初始化的最佳方法是什么:

let x = TryCalculateX() // for example, searching in DB
if x = null then
    // for example, we create it ourself and out it into DB
    let x = TryAnotherCalcultaion() 
    someProcessing x // x is now initialized => Ok
someProcessing x // here I use old, not initialized x! => Fails

你如何处理类似情况?

UPD。我设计的最好的是:

let getX() =
    let res = TryCalculateX() 
    if res = null then
        let res = TryAnotherCalcultaion() 
        res
    else
        res

这不是很酷,恕我直言

更新2. @ChaosPandion提出了很好的解决方法:

let x = 
    match TryCalculateX() with
    | null -> TryAnotherCalculation() 
    | x -> x
someProcessing x

但是如果添加一个额外的嵌套级别,这也不是很好:

let x = 
    match TryCalculateX() with
    | null -> 
        match TryAnotherCalculation()  with
        | null -> OneMoreCalculation()
        | y -> y
    | x -> x
someProcessing x

也许还有一些更普遍的模式可供申请?

更新3.再次感谢@ChaosPandion这是一个通用解决方案:

// Different initialization strategies
let x() = printfn("x"); None
let y() = printfn("y"); None
let z() = printfn("z"); Some(1)

// Sequence of strategies
let a = seq { 
    yield x()
    yield y()
    yield z()
}

// Initializing function
let init what = Seq.find (fun el -> Option.isSome el) what

// Initializing
let b = init a

F#交互式打印:

  

x y z ... val b:int option = Some 1

1 个答案:

答案 0 :(得分:5)

以下是一个很好的约定。它可以与null一起使用,但我建议您返回'a option类型。

方法

let x = 
    match TryCalculateX() with
    | Some x -> x
    | None -> TryAnotherCalculation() 
someProcessing x

<强>空

let x = 
    match TryCalculateX() with
    | null -> TryAnotherCalculation() 
    | x -> x
someProcessing x

<强>序列

假设您写下每次尝试返回option,那么您可以编写优雅的尝试序列。

let x = seq {
            yield firstTry ()
            yield secondTry ()
            yield thirdTry ()
            yield sensibleDefault
        } |> Seq.pick id
someProcessing x