March 01, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | On Tue, 01 Mar 2005 11:52:33 +0100, Anders F Björklund <afb@algonet.se> wrote: > Regan Heath wrote: > >>> Mixing and matching different styles in one call sounds like a >>> good way to get hopelessly confused about which rules apply... >> "styles"? >> I assume you mean, "printing with formatting" and "printing without formatting'? > > Yes, as in printf(3) vs. java.lang.System.out.println > >> I don't think "printing without formatting" exists. Instead "printing in the default specified format" is what we do. > > Which was why I hacked "printing without formatting" in :-) I think we're arguing semantics. I would say "printing with no formatting options". We probably mean the same thing. >> I don't see how it's confusing to want to print 'a' in a specified format and 'b' in the default specified format in one function call. eg. >> printf("%d %f",a,b); >> prints a and b in the default format. >> printf("%05d %f",a,b); >> prints a in the specified format and b in the default format. >> You still have to know what the default format is. > > Those are all good... (examples of using C-style printf) > > What I meant was confusing was when you mix and match, like in: > > writefln("%5s",a,b,i,"%.6f",f); > > But that could just be me not being used to D's writef just yet... > (not too strange really, since it hasn't been around all that long?) I prefer the above as the arguments appear in the order they are printed.. no more counting args and format specifiers, ensuring they match up to the types passed, finding the one that appears 3rd, etc. > There's also *another* way (Java) of doing the same thing: > > System.out.println(a + " " + b); > > Which in D would become something like: > > std.stdio.writeln > (std.string.toString(a) ~ " " ~ std.string.toString(b)); > > The formatting options totally sucks here, of course. > (It's a "known bug" of Java, before JDK 1.5... :-) ) > > But it doesn't have any "problems" with percent strings ? True. But it's inefficient. It builds a new string in order to print it out. >>> Having both in the language is good, but in one function ? Nah. >> Perhaps. I'm not convinced yet. > > It's not like writef is going away, or anything... True, and maybe having different functions is the best solution. We'll see.. Regan |
March 01, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath wrote: >>> I don't think "printing without formatting" exists. Instead "printing in the default specified format" is what we do. >> >> Which was why I hacked "printing without formatting" in :-) > > I think we're arguing semantics. I would say "printing with no formatting options". We probably mean the same thing. I meant that stdio.writeln didn't use to exist ? But I added it. writef works like printf in C. writeln works like println in Java. >> There's also *another* way (Java) of doing the same thing: >> >> System.out.println(a + " " + b); >> >> Which in D would become something like: >> >> std.stdio.writeln >> (std.string.toString(a) ~ " " ~ std.string.toString(b)); >> >> The formatting options totally sucks here, of course. >> (It's a "known bug" of Java, before JDK 1.5... :-) ) >> >> But it doesn't have any "problems" with percent strings ? > > True. But it's inefficient. It builds a new string in order to print it out. Since Java is Designed-to-be-Slow™, it doesn't concern itself with such trivial matters (but + does have some optimizations to convert into StringBuffer.append and other such hacks*...) And in D, you can instead use the variadic args syntax: writeln(toString(a), " ", toString(b)); It still won't choke on any percentages or require "%s". Never said I copied it over from Java *exactly*, did I ? :-) --anders * PS. Here is what the Java disassembly looks like: > Code: > 0: bipush 42 > 2: istore_1 > 3: ldc #9; //float 3.1415f > 5: fstore_2 > 6: getstatic #13; //Field java/lang/System.out:Ljava/io/PrintStream; > 9: new #16; //class StringBuffer > 12: dup > 13: invokespecial #21; //Method java/lang/StringBuffer."<init>":()V > 16: iload_1 > 17: invokevirtual #25; //Method java/lang/StringBuffer.append:(I)Ljava/lang/StringBuffer; > 20: bipush 32 > 22: invokevirtual #28; //Method java/lang/StringBuffer.append:(C)Ljava/lang/StringBuffer; > 25: fload_2 > 26: invokevirtual #31; //Method java/lang/StringBuffer.append:(F)Ljava/lang/StringBuffer; > 29: invokevirtual #35; //Method java/lang/StringBuffer.toString:()Ljava/lang/String; > 32: invokevirtual #39; //Method java/io/PrintStream.println:(Ljava/lang/String;)V > 35: return For the sample code, that we used earlier: public class p { public static void main(String[] args) { int a = 42; float b = 3.1415f; System.out.println(a + " " + b); } } |
March 01, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | Anders F Björklund wrote: > I meant that stdio.writeln didn't use to exist ? But I added it. > And in D, you can instead use the variadic args syntax: > writeln(toString(a), " ", toString(b)); > It still won't choke on any percentages or require "%s". > Never said I copied it over from Java *exactly*, did I ? :-) Or that I implemented it correctly, for that manner... :-P In the patch it should be "continue" instead of "return", or it will just print the first string argument and stop. > if (ignorePercent) > { > foreach(dchar c; fmt) > putc(c); > continue; > } --anders |
March 01, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath wrote:
> On Tue, 01 Mar 2005 11:52:33 +0100, Anders F Björklund <afb@algonet.se> wrote:
>
>> There's also *another* way (Java) of doing the same thing:
>>
>> System.out.println(a + " " + b);
>>
>> Which in D would become something like:
>>
>> std.stdio.writeln
>> (std.string.toString(a) ~ " " ~ std.string.toString(b));
>>
>> The formatting options totally sucks here, of course.
>> (It's a "known bug" of Java, before JDK 1.5... :-) )
>>
>> But it doesn't have any "problems" with percent strings ?
>
>
> True. But it's inefficient. It builds a new string in order to print it out.
>
There's no need to: if Anders' writeln works as I expect, it could become: std.stdio.writeln(a," ",b). Internally, it will create a new string, but so does writefln.
_______________________
Carlos Santander Bernal
|
March 01, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | On Tue, 01 Mar 2005 23:29:36 +0100, Anders F Björklund <afb@algonet.se> wrote: > Regan Heath wrote: > >>>> I don't think "printing without formatting" exists. Instead "printing in the default specified format" is what we do. >>> >>> Which was why I hacked "printing without formatting" in :-) >> I think we're arguing semantics. I would say "printing with no formatting options". We probably mean the same thing. > > I meant that stdio.writeln didn't use to exist ? But I added it. I know! Forget it, it's not an important point that I was trying to make. > writef works like printf in C. No, it doesn't, not exactly, as I said before. printf only accepts the first string as a format string. writef treats every string parameter (unless it's a parameter specified by %s in a previous format string) as a format string. the difference is likely to cause a few bugs, till people get used to it. >>> There's also *another* way (Java) of doing the same thing: >>> >>> System.out.println(a + " " + b); >>> >>> Which in D would become something like: >>> >>> std.stdio.writeln >>> (std.string.toString(a) ~ " " ~ std.string.toString(b)); >>> >>> The formatting options totally sucks here, of course. >>> (It's a "known bug" of Java, before JDK 1.5... :-) ) >>> >>> But it doesn't have any "problems" with percent strings ? >> True. But it's inefficient. It builds a new string in order to print it out. > > Since Java is Designed-to-be-Slow™, it doesn't concern itself > with such trivial matters (but + does have some optimizations > to convert into StringBuffer.append and other such hacks*...) Sure. Hacks added to cover up bad design IMO. But really, this is totally beside the point. > And in D, you can instead use the variadic args syntax: > writeln(toString(a), " ", toString(b)); > It still won't choke on any percentages or require "%s". > > Never said I copied it over from Java *exactly*, did I ? :-) Never suggested you did. Regan |
March 01, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | On Tue, 01 Mar 2005 17:16:26 -0500, Carlos Santander B. <csantander619@gmail.com> wrote:
> Regan Heath wrote:
>> On Tue, 01 Mar 2005 11:52:33 +0100, Anders F Björklund <afb@algonet.se> wrote:
>>
>>> There's also *another* way (Java) of doing the same thing:
>>>
>>> System.out.println(a + " " + b);
>>>
>>> Which in D would become something like:
>>>
>>> std.stdio.writeln
>>> (std.string.toString(a) ~ " " ~ std.string.toString(b));
>>>
>>> The formatting options totally sucks here, of course.
>>> (It's a "known bug" of Java, before JDK 1.5... :-) )
>>>
>>> But it doesn't have any "problems" with percent strings ?
>> True. But it's inefficient. It builds a new string in order to print it out.
>>
>
> There's no need to: if Anders' writeln works as I expect, it could become: std.stdio.writeln(a," ",b). Internally, it will create a new string, but so does writefln.
You're right, and it does. This wasn't the point I was trying to make, we were side tracked.
:)
Regan
|
March 02, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath wrote: > writef treats every string parameter (unless it's a parameter specified by %s in a previous format string) as a format string. I know... (discovered it the hard way, with the "invalid specifier") What I meant was that it still needs a format string, like printf does. Even if every other string parameter can now be a format, if need be... > the difference is likely to cause a few bugs, till people get used to it. It's just that some people find the format a bit hard in the beginning. Not having to scan for format % is a teeny bit faster too, I suppose ? Don't get me wrong, writef is good. But it could be overkill for some ? --anders |
March 02, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | On Wed, 02 Mar 2005 01:14:15 +0100, Anders F Björklund <afb@algonet.se> wrote: > Regan Heath wrote: > >> writef treats every string parameter (unless it's a parameter specified by %s in a previous format string) as a format string. > > I know... (discovered it the hard way, with the "invalid specifier") > > What I meant was that it still needs a format string, like printf does. > Even if every other string parameter can now be a format, if need be... The fact that every other string can be a format string, means that for every string you want to print you have to use a format string eg "%s" _or_ know for certain the string contains no % in it. This seems like a step back to me, so, while writef is a step forward in most areas, here it's a step backward, it's harder to use correctly. Sure, there are advantages, I mentioned them earlier. >> the difference is likely to cause a few bugs, till people get used to it. > > It's just that some people find the format a bit hard in the beginning. True. > Not having to scan for format % is a teeny bit faster too, I suppose ? Yep. > Don't get me wrong, writef is good. But it could be overkill for some ? Sure, don't get me wrong. I 'like' the new write function. My complaint is with writef. The new behaviour (as compared to printf) has both good and bad effects IMO. Can we remove the bad ones? leaving only good ones? Regan |
March 02, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath wrote: > The fact that every other string can be a format string, means that for every string you want to print you have to use a format string eg "%s" _or_ know for certain the string contains no % in it. I haven't read the article* that Walter wrote in DDJ about it (writef), but that does seem to be by design - if you read the description at: http://www.digitalmars.com/d/std_format.html > My complaint is with writef. The new behaviour (as compared to printf) has both good and bad effects IMO. Can we remove the bad ones? leaving only good ones? I think writef is OK, just wonder whatever happened to "readf" ? Waiting for a new DDJ issue, perhaps ? ;-) Or perhaps out varargs... Otherwise pointers is the only way to do a scanf(3) workalike in D. --anders * http://www.ddj.com/documents/ddj0501e/ ($$$) > "printf Revisited" > Walter Bright > The D programming language addresses some shortcomings > of C and C++’s venerable printf. |
March 02, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | On Wed, 02 Mar 2005 01:52:38 +0100, Anders F Björklund <afb@algonet.se> wrote:
> Regan Heath wrote:
>
>> The fact that every other string can be a format string, means that for every string you want to print you have to use a format string eg "%s" _or_ know for certain the string contains no % in it.
>
> I haven't read the article* that Walter wrote in DDJ about it (writef),
> but that does seem to be by design - if you read the description at:
> http://www.digitalmars.com/d/std_format.html
I agree it's working how it's supposed to work. I just don't like how it works.
Regan
|
Copyright © 1999-2021 by the D Language Foundation