从 F# 调用 DateTime.TryParse 的正确方法是什么? [英] What is the correct way to call DateTime.TryParse from F#?
问题描述
从 F# 调用 DateTime.TryParse 的正确方法是什么?我正在尝试从 F# Interactive 测试一些代码,但我不知道如何通过 ref 将可变的 DateTime 传递给第二个参数.F# 中的 in/out/ref 语法是什么?
What is the correct way to call DateTime.TryParse from F#? I am trying to test some code from F# interactive and I can't figure out how to pass a mutable DateTime into the second argument by ref. What is the in/out/ref syntax in F#?
这是我正在查看的方法签名:http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx?cs-save-lang=1&cs-lang=fsharp#code-snippet-1
This is the method signature I'm looking at: http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx?cs-save-lang=1&cs-lang=fsharp#code-snippet-1
推荐答案
Chris 的回答是正确的,如果您确实需要通过引用传递可变的 DateTime
.但是,在 F# 中使用编译器将尾随 out
参数视为元组返回值的能力更为惯用:
Chris's answer is correct if you really need to pass a mutable DateTime
by reference. However, it is much more idiomatic in F# to use the compiler's ability to treat trailing out
parameters as tupled return values:
let couldParse, parsedDate = System.DateTime.TryParse("11/27/2012")
这里,第一个值是 bool
返回值,而第二个值是分配的 out 参数.
Here, the first value is the bool
return value, while the second is the assigned out parameter.
这篇关于从 F# 调用 DateTime.TryParse 的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!