www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - printf and global namespace

reply mclysenk mtu.edu writes:
Does printf really need to be in the global namespace anymore?  Now that writef
is  fully developed, it seems obsolete.  I would say that using printf in a
newer D program is bad style, since writef is much safer and neater.  It also
gives new programmers the wrong idea.  Often they'll try to printf dynamic
character arrays with a "%s", which surprisingly works most of the time, but
fails whenever the string has no null terminators.

The only disadvantage to this would be breaking some older programs, but they
could be fixed by just adding an import std.c.stdio .  I say that all of the old
C stdio functions should get out of the global namespace, and back into their
module where they belong.

-Mik
Apr 28 2006
next sibling parent reply Don Clugston <dac nospam.com.au> writes:
mclysenk mtu.edu wrote:
 Does printf really need to be in the global namespace anymore?  Now that writef
 is  fully developed, it seems obsolete.  I would say that using printf in a
 newer D program is bad style, since writef is much safer and neater.  It also
 gives new programmers the wrong idea.  Often they'll try to printf dynamic
 character arrays with a "%s", which surprisingly works most of the time, but
 fails whenever the string has no null terminators.
 
 The only disadvantage to this would be breaking some older programs, but they
 could be fixed by just adding an import std.c.stdio .  I say that all of the
old
 C stdio functions should get out of the global namespace, and back into their
 module where they belong.
 
 -Mik

I agree. It's a newbie trap. I consider this to be entirely different from the recently discussed issue about whether it's OK for Object to use printf. Personally, I'm don't really care about how Object is implemented, but I think it's a real problem that an obsolete function is implicitly declared in every D program. One issue, though, is the use of debug printf() all through Phobos. Since there's no 'debug import' statement, we could create a file std.c.debugstdio consisting of: ------------------------- module std.c.debugstdio; version(debug) { extern (C) { /// C's printf function. int printf(char *, ...); } } ------------------------- and add this to the top of every Phobos file.
Apr 28 2006
parent reply clayasaurus <clayasaurus gmail.com> writes:
Don Clugston wrote:
 Since there's no 'debug import' statement, we could

I use 'debug import std.stdio;' statement all the time *confused*
Apr 28 2006
next sibling parent pragma <pragma_member pathlink.com> writes:
In article <e2tdip$2oi1$1 digitaldaemon.com>, clayasaurus says...
Don Clugston wrote:
 Since there's no 'debug import' statement, we could

I use 'debug import std.stdio;' statement all the time *confused*

Ditto. And since it would be for debug only, I don't see the problem of stdio being pulled in automatically when building a debug phobos. - EricAnderton at yahoo
Apr 28 2006
prev sibling parent Don Clugston <dac nospam.com.au> writes:
clayasaurus wrote:
 Don Clugston wrote:
 Since there's no 'debug import' statement, we could

I use 'debug import std.stdio;' statement all the time *confused*

<remove foot from mouth before speaking next time> I was very tired and not thinking clearly </remove foot from mouth before speaking next time>.
Apr 28 2006
prev sibling next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Mik wrote:

 Does printf really need to be in the global namespace anymore?

No, but Walter likes it while debugging so it stays in there...
 Now that writef
 is  fully developed, it seems obsolete.  I would say that using printf in a
 newer D program is bad style, since writef is much safer and neater.  It also
 gives new programmers the wrong idea.  Often they'll try to printf dynamic
 character arrays with a "%s", which surprisingly works most of the time, but
 fails whenever the string has no null terminators.
 
 The only disadvantage to this would be breaking some older programs, but they
 could be fixed by just adding an import std.c.stdio .  I say that all of the
old
 C stdio functions should get out of the global namespace, and back into their
 module where they belong.

I agree completely. The only thing missing from "writef" is a fully implemented "readf", or perhaps a "write" version that works without the format characters. But printf must die! http://www.digitalmars.com/d/archives/digitalmars/D/bugs/5838.html --anders
Apr 28 2006
parent Sean Kelly <sean f4.ca> writes:
Anders F Björklund wrote:
 
 The only thing missing from "writef" is a fully implemented "readf",
 or perhaps a "write" version that works without the format characters.

There's an implementation here: http://www.home.f4.ca/sean/d/ It could probably be improved as it uses dchars internally, but it's a complete and correct implementation of scanf from the C99 spec plus some additional support for D types. Throwing exceptions out of the function could be accomplished easily by rethrowing from the appropriate catch block at the bottom of unFormat. Please note that this currently requires the updated utf module it contains, though I believe it could be rewritten to grab a single char and use stride() to determine how many more were expected--I wasn't aware of stride() when I wrote this code a year or so ago. If there were real interest in getting this into Phobos I could probably find the time to make any requested updates myself, but I'm too busy to do so for any other reason. Sean
Apr 28 2006
prev sibling parent reply Lionello Lunesu <lio lunesu.remove.com> writes:
mclysenk mtu.edu wrote:
 Does printf really need to be in the global namespace anymore?  Now that writef
 is  fully developed, it seems obsolete.  I would say that using printf in a
 newer D program is bad style, since writef is much safer and neater.  It also
 gives new programmers the wrong idea.  Often they'll try to printf dynamic
 character arrays with a "%s", which surprisingly works most of the time, but
 fails whenever the string has no null terminators.

Agreed! It even complicates overloading printf for output to a file or window. For easy debugging it would be better to add a void println(char[]) function. That's what newbies really want, not printf("%.*s\n",x) and also no writefln("%s",x) to handle %* in the string correctly. A simple print[ln] would have the added benefit of adding a low foot-print to the executable when used. I know printf's not that big, but still, it seems way to heavy for simple printing. puts seems similar to print but (1) uses zero-terminated strings and (2) no way to print text without the new-line. L.
May 02 2006
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Lionello Lunesu wrote:

 For easy debugging it would be better to add a void println(char[]) 
 function. That's what newbies really want, not printf("%.*s\n",x) and 
 also no writefln("%s",x) to handle %* in the string correctly.

Q: What was wrong with "std.stdio.writeln", as the name for it ? Just seemed more fitting, writef (with format) => write (without) My old stdio hacks are still at http://www.algonet.se/~afb/d/stdio/, [see digitalmars.D/21692] and Sean Kelly's versions are still at http://www.home.f4.ca/sean/d/ [see digitalmars.D/11021] But even if std.stdio isn't cleaned up, "printf" should still die! (again, that is: in object.d - it should move back to std.c.stdio) Otherwise you get this: "maybe I should use writef, but since I have to use an import then - I think I'll just use implicit printf instead" mentality, and we'll have to insert those "import std.c.stdio;" later ? I've already done so for Phobos once, but it is needed to do so again. I think the offical Digital Mars docs should "lead by example", and use: import std.stdio; void main() { writefln("Hello, World!"); // <-- Even better, use "writeln" here } Instead of the current hacks, which just looks like C always has done: int main() { printf("hello world\n"); return 0; } It leads people to think "oh, printf works with D strings" --> *KABOOM* --anders
May 02 2006
parent reply Lionello Lunesu <lio lunesu.remove.com> writes:
Anders F Björklund wrote:
 Lionello Lunesu wrote:
 
 For easy debugging it would be better to add a void println(char[]) 
 function. That's what newbies really want, not printf("%.*s\n",x) and 
 also no writefln("%s",x) to handle %* in the string correctly.

Q: What was wrong with "std.stdio.writeln", as the name for it ? Just seemed more fitting, writef (with format) => write (without)

I just miss "print" from the old days, but you're right, write[ln] is pretty clear :) Maybe we can rid of the ?: operator and use ? for printing once again! void main () { ?"hello world" } (It's a joke :S)
 It leads people to think "oh, printf works with D strings" --> *KABOOM*

True! That's the main reason to move it AFAIC! L.
May 03 2006
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Lionello Lunesu wrote:

 Maybe we can rid of the ?: operator and use ? for printing once again!
 
 void main () { ?"hello world" }

Hmm,think we should go all the way then. What about line numbers ? :-P 10 ? "hello world" 20 GOTO 10 30 REM make it stop --anders
May 04 2006
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Anders F Björklund wrote:
 Lionello Lunesu wrote:
 
 Maybe we can rid of the ?: operator and use ? for printing once again!

 void main () { ?"hello world" }

Hmm,think we should go all the way then. What about line numbers ? :-P 10 ? "hello world" 20 GOTO 10 30 REM make it stop --anders

Bah! If you're going to go that far, you might as well go the whole-hog: THE LESSER-KNOWN PROGRAMMING LANGUAGES #10: SIMPLE SIMPLE is an acronym for Sheer Idiot's Monopurpose Programming Language Environment. This language, developed at the Hanover College for Technological Misfits, was designed to make it impossible to write code with errors in it. The statements are, therefore, confined to BEGIN, END and STOP. No matter how you arrange the statements, you can't make a syntax error. Programs written in SIMPLE do nothing useful. Thus they achieve the results of programs written in other languages without the tedious, frustrating process of testing and debugging. -- Daniel "ph34r my l33t fortune-fu" Keep -- v1sw5+8Yhw5ln4+5pr6OFma8u6+7Lw4Tm6+7l6+7D a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
May 04 2006