简体   繁体   English

F# - printfn Guid

[英]F# - printfn Guid

I have following printing example in F# 我在F#中有以下打印示例

for row in data.Rows do 
     printfn "Example: (%s)" row.A

But I received this error 但是我收到了这个错误

Script1.fsx(15,67): error FS0001: This expression was expected to have type string but here has type Guid Script1.fsx(15,67):错误FS0001:此表达式应具有类型字符串,但此处具有类型Guid

I didn't find any example of printing of Guid type. 我没有找到任何打印Guid类型的例子。 I tried apply ToString() method to row.A but it is not working as well 我尝试将ToString()方法应用于row.A但它也不能正常工作

There are two options here: 这里有两个选项:

for row in data.Rows do 
     printfn "Example: (%s)" (row.A.ToString())

or 要么

for row in data.Rows do 
     printfn "Example: (%A)" row.A

Here the %A can be used for any type and the compiler will automatically print it for you 这里%A可用于任何类型,编译器将自动为您打印

You can use %A for a Guid, like this: 您可以将%A用于Guid,如下所示:

printfn "Example: (%A)" row.A

When using %s , the type must be a string. 使用%s ,类型必须是字符串。

The MSDN Documentation has more information about which format type to use and how it behaves. MSDN文档提供了有关要使用的格式类型及其行为方式的详细信息。

In addition to the answers already given, it's worth noting that Guid also supports an overload of ToString , giving you the option to control how the GUID string is formatted. 除了已经给出的答案之外,值得注意的是Guid还支持ToString的重载,让您可以选择控制GUID字符串的格式。

You could, for example, write it out like this: 例如,您可以像这样写出来:

printfn "Example: (%s)" (row.A.ToString "n")

if you want to omit the hyphens ( Example: (78e6fb89dc5045988d445c4d8aef4e28) ). 如果你想省略连字符( Example: (78e6fb89dc5045988d445c4d8aef4e28) )。

Or you could use stringf for this, if you want an alternative with pipes instead of parentheses: 或者你可以使用stringf ,如果你想要一个管道而不是括号的替代品:

row.A |> stringf "n" |> printfn "Example: (%s)"

or if you don't need the formatting option: 或者如果您不需要格式化选项:

row.A |> string |> printfn "Example: (%s)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM