简体   繁体   中英

F# ToString() %A formatting doesnt work on linux with mono

I use the fsharpc compiler installed with mono. And I wanted to override the ToString() method for some of my types. The thing is it all compiles just fine but printf "%A" just gives me the default debug output. So to make absolutly sure i didnt miss something, I wrote a simple test on try.fsharp.org and compiled the same code myself. Still just getting default formatting

So is there something missing from the .net framework or something?

To ensure that printf calls ToString() , use %O format instead of %A :

type MyType =
    {
        MyValue : string
    }
    override __.ToString() = "Hello!"

[<EntryPoint>]
let main argv =  
    let record = { MyValue = "A value" }
    printfn "%A" record   // { MyValue = "A value" }
    printfn "%O" record   // Hello!
    0

See this link for details.

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