Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/fsharp/FSharp.Core/prim-types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4555,8 +4555,13 @@ namespace Microsoft.FSharp.Core
| _ -> value.ToString()

// other commmon mscorlib reference types
when 'T : StringBuilder = let x = (# "" value : StringBuilder #) in x.ToString()
when 'T : IFormattable = let x = (# "" value : IFormattable #) in x.ToString(null, CultureInfo.InvariantCulture)
when 'T : StringBuilder =
if value = unsafeDefault<'T> then ""
else let x = (# "" value : StringBuilder #) in x.ToString()

when 'T : IFormattable =
if value = unsafeDefault<'T> then ""
else let x = (# "" value : IFormattable #) in x.ToString(null, CultureInfo.InvariantCulture)

[<NoDynamicInvocation(isLegacy=true)>]
[<CompiledName("ToChar")>]
Expand Down
9 changes: 9 additions & 0 deletions tests/FSharp.Core.UnitTests/FSharp.Core/OperatorsModule2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace FSharp.Core.UnitTests.Operators

open System
open System.Text
open System.Globalization
open System.Threading

Expand Down Expand Up @@ -754,6 +755,12 @@ type OperatorsModule2() =
let result = Operators.string (null:string)
Assert.AreEqual("", result)

let result = Operators.string (null:StringBuilder)
Assert.AreEqual("", result)

let result = Operators.string (null:IFormattable)
Assert.AreEqual("", result)

// value type
let result = Operators.string 100
Assert.AreEqual("100", result)
Expand Down Expand Up @@ -801,6 +808,8 @@ type OperatorsModule2() =
// reset the culture
Thread.CurrentThread.CurrentCulture <- currentCI



[<Fact>]
member _.``string: don't raise FS0670 anymore``() =
// The type used here, when compiled, should not raise this error:
Expand Down