Semantics of toString

Yigal Chripun yigal100 at gmail.com
Fri Nov 6 09:51:13 PST 2009


On 06/11/2009 15:38, Leandro Lucarella wrote:
> Yigal Chripun, el  6 de noviembre a las 14:23 me escribiste:
>>>> the c style format string that specifies types is a horrible horrible
>>>> thing and should be removed.
>>
>>> How do you do %.3f in {}-notation?
>>
>> writefln("{0:F3}", value);
>>
>>> Your formatting string should be written as writeln(ansi, " ", iso);
>
> This is horrible, horrible for internationalization, you just can't assume
> how a language order words.
>
> Anyway, about the type in the format, I think it's nice, as you just
> proved, tango have it too "{0:F3}" is saying "treat the value as a float
> and format it that way". The deal is, the type should not be used to know
> the size of the parameter in the stack like in C's printf(), it should be
> just a hint to convert the value to another type.
>
> So, type specification is important. Variables reordering is important
> too, and you even have it in POSIX's printf():
>
> 	printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);
>
> (see http://www.opengroup.org/onlinepubs/9699919799/functions/printf.html)
>
> I like printf()'s format (I don't know if it's just because I'm used to it
> though :).
>


F in the above is _not_ a type specifier. It is a format specifier that 
means "fixed". More over, each type defines it's own format specifiers, 
and there's also a way to custom format stuff.
Here's some more examples: (from MSDN)

string myName = "Fred";
Console.WriteLine(String.Format("Name = {0}, hours = {1:hh}, minutes = 
{1:mm}", myName, DateTime.Now));
// Depending on the current time, the example displays output like the 
following:
//    Name = Fred, hours = 11, minutes = 30

string FormatString1 = String.Format("{0:dddd MMMM}", DateTime.Now);
string FormatString2 = DateTime.Now.ToString("dddd MMMM");

Console.WriteLine("{0:F}", DateTime.Now); // NOT float
// F for DateTime means Full date/time pattern (long time).

Another issue with the .NET design is that it's locale aware.
e.g.

// Display using pt-BR culture's short date format
DateTime thisDate = new DateTime(2008, 3, 15);
CultureInfo culture = new CultureInfo("pt-BR");
Console.WriteLine(thisDate.ToString("d", culture));  // Displays 15/3/2008

besides, the printf format is plain unreadable. It's like comparing 
ASCII to Unicode - D moved to native Unicode support and should move to 
this much better design as well.




More information about the Digitalmars-d mailing list