简体   繁体   中英

F# - printfn Guid

I have following printing example in 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

I didn't find any example of printing of Guid type. I tried apply ToString() method to row.A but it is not working as well

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

You can use %A for a Guid, like this:

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

When using %s , the type must be a string.

The MSDN Documentation has more information about which format type to use and how it behaves.

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.

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) ).

Or you could use stringf for this, if you want an alternative with pipes instead of parentheses:

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

or if you don't need the formatting option:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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