F# string operator blows up on guids


I was under impression that in F# the following two lines are supposed to give identical results:

let a = string v
let a = v.ToString()

It is implied that v is an object. It turns out that if v is a System.Guid the first line just throws an exception:

System.FormatException occurred
Message="Format String can be only \"D\", \"d\", \"N\", \"n\", \"P\", \"p\", \"B\" or \"b\"."
   at System.Guid.ToString(String format, IFormatProvider provider)

I can certainly deal with Guids separately, the question is what other objects will give me the same trouble? Should I avoid using the string operator at all?
In my case the object potentially can be anything

Best Solution

This is a bug that is (will be) fixed in the next release.

(In general, it should work; the bug is because System.Guid does not respond to the IFormattable "G" specifier, despite the fact that the docs for IFormattable say that all implementers must implement the "G" specifier. So it's actually kinda a bug in System.Guid, but the F# library will work around this bug in its 'string' operator in the next release.

In short, you can use this operator safely, except for Guid right now, but that will be fixed soon. In the meantime you can special-case Guid.)

Related Question