Is this F # structured printf error?

the code:

printfn "%10s"  "abc"
printfn "%-10s" "abc"
printfn "%10d"   123
printfn "%-10d"  123
printfn "%10c"   'a'
printfn "%-10c"  'a'

Output:

       abc
abc
       123
123
a
a

So correct alignment %cdoes not work as I expect ...

F# 2.0 build 4.0.30319.1

+5
source share
2 answers

I do not think that the fact that it is not in the table of valid types causes a problem (the "% c" modifier is definitely supported, because the type printfn "%c"is equal char -> unit, as you would expect). So it looks like a mistake to me.

A simple solution is to use the "% O" modifier, which accepts any type (also including values char) and formats it using the method ToString(available for all .NET types):

> printfn "%10O" 'a';;
         a
val it : unit

BTW: F # ( CTP), printf.fs ( 478):

| 's',nobj::args -> 
  formatString outputChar info width (unbox nobj) false; i+1,args
| 'c',nobj::args -> 
  outputChar (unbox nobj); i+1,args // (1)
| 'b',nobj::args -> 
  formatString outputChar info width 
    (if (unbox nobj) then "true" else "false") false; i+1,args
| 'O',xobj::args -> 
  formatString outputChar info width 
    (match xobj with null -> "<null>" | _ -> xobj.ToString()) false; i+1,args

(1) width ( ). , ( - , , ?), !

, (;-)! ?):

| 'c', nobj::args -> 
  formatString outputChar info width 
    (string ((unbox nobj):char)) false; i+1,args 
+6

% c F # printf. , .

, .

+3

All Articles