【发布时间】:2020-02-09 04:13:42
【问题描述】:
我正在做一些 F# 练习,任务是实现一个名为 clean 的函数,该函数接受字符串输入并返回解析后的数字。
let clean input =
input
|> Seq.filter Char.IsDigit
|> Seq.fold (fun acc cur ->
if acc = "" && cur = '1' then
acc
else acc + string cur) ""
|> uint64
|> Ok
这是我的临时解决方案。但是下面的单元测试失败了:
[<Fact>]
let ``Cleans the number`` () =
let expected: Result<uint64,string> = Ok 2234567890UL
let parsed = clean "(223) 456-7890"
parsed |> should equal expected
失败了:
FsUnit.Xunit+MatchException : 异常类型 'FsUnit.Xunit+MatchException' 被抛出。预期:等于确定 2234567890UL 实际:是 Microsoft.FSharp.Core.FSharpResult
2[System.UInt64,System.Object]1 匹配器)
at FsUnit.Xunit.Assert.That.Static[a](a actual, IMatcher
在PhoneNumberTest.Cleans 中带有dots() 的数字
所以我加了一个类型注解:let clean input: Result<uint64,string> // function body is the same
我可以避免类型注释吗?第一个解决方案失败的技术原因是什么?
【问题讨论】:
标签: types f# annotations