Jump to page: 1 2
Thread overview
printf and global namespace
Apr 28, 2006
mclysenk
Apr 28, 2006
Don Clugston
Apr 28, 2006
clayasaurus
Apr 28, 2006
pragma
Apr 29, 2006
Don Clugston
Apr 28, 2006
Sean Kelly
May 02, 2006
Lionello Lunesu
May 04, 2006
Lionello Lunesu
May 04, 2006
Daniel Keep
April 28, 2006
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


April 28, 2006
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.
April 28, 2006
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
April 28, 2006
Don Clugston wrote:
> Since there's no 'debug import' statement, we could

I use 'debug import std.stdio;' statement all the time *confused*
April 28, 2006
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
April 28, 2006
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
April 29, 2006
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>.
May 02, 2006
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
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 http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/21692]
and Sean Kelly's versions are still at http://www.home.f4.ca/sean/d/
[see http://www.digitalmars.com/drn-bin/wwwnews?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 04, 2006
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.
« First   ‹ Prev
1 2