如何从字符串创建some string?
我正在通过F# Koans教程,我是不是被困在了这个上面:
[<Koan>]
let ProjectingValuesFromOptionTypes() =
let chronoTrigger = { Name = "Chrono Trigger"; Platform = "SNES"; Score = Some 5 }
let halo = { Name = "Halo"; Platform = "Xbox"; Score = None }
let decideOn game =
game.Score
|> Option.map (fun score -> if score > 3 then "play it" else "don't play")
//HINT: look at the return type of the decide on function
AssertEquality (decideOn chronoTrigger) (Some "play it")
AssertEquality (decideOn halo) (Some "don't play")我得到的例外是:
You have not yet reached enlightenment ...
Expected: null
But was: <Some(don't play)>如何将字符串向上转换为option string类型
发布于 2018-01-31 20:49:32
如何将字符串向上转换为类型的选项字符串?
铸造有一个非常特殊的意义。您想要做的是用一个string包装您的Option,而不是转换它。为此,请使用Some构造函数:
let x = Some myString //x: string option但是,我不认为这将修复您正在获得的断言错误(至少,它本身不会)。我不想在这里给出完整的答案(特别是因为这不是您要问的,并且找到答案是做Koan的全部意义),但是我将留下这个线索,说明为什么您在断言中看到了一个null:
None |> printfn "Value: %A" // Value: <null>有关该行为的更多信息,请参见为什么没有一个表示为null?。
https://stackoverflow.com/questions/48550980
复制相似问题