View mode: basic / threaded / horizontal-split · Log in · Help
February 20, 2005
writef is good, printf is bad
printf is old, bad and ugly. Theoretically it is correct. Let's try writef.
What should I do, when a char[] is actually empty (ie. null) ?

//char[] firstpart="kakukk";
char[] firstpart="";

printf("firstpart=%.*s\n",firstpart); //works, shows nothing after = sign

writefln("firstpart=%s",firstpart); //aborts and rewards with this message

Runtime error:
firstpart=Error: std.format formatArg

Could anybody show me a workaround?

Thanks in advance,
Tamas Nagy
February 20, 2005
Re: writef is good, printf is bad
On Sun, 20 Feb 2005 11:44:42 +0000, MicroWizard wrote:

> printf is old, bad and ugly. Theoretically it is correct. Let's try writef.
> What should I do, when a char[] is actually empty (ie. null) ?
> 
> //char[] firstpart="kakukk";
> char[] firstpart="";
> 
> printf("firstpart=%.*s\n",firstpart); //works, shows nothing after = sign
> 
> writefln("firstpart=%s",firstpart); //aborts and rewards with this message
> 
> Runtime error:
> firstpart=Error: std.format formatArg
> 
> Could anybody show me a workaround?
> 
> Thanks in advance,
> Tamas Nagy

I don't get that runtime error using writefln() with a firstpartk="". 
Works fine on dmd 0.113 on Linux.

My output:

firstpart =
February 20, 2005
Re: writef is good, printf is bad
On Sun, 20 Feb 2005 11:44:42 +0000 (UTC), MicroWizard wrote:

> printf is old, bad and ugly. Theoretically it is correct. Let's try writef.
> What should I do, when a char[] is actually empty (ie. null) ?
> 
> //char[] firstpart="kakukk";
> char[] firstpart="";
> 
> printf("firstpart=%.*s\n",firstpart); //works, shows nothing after = sign
> 
> writefln("firstpart=%s",firstpart); //aborts and rewards with this message
> 
> Runtime error:
> firstpart=Error: std.format formatArg
> 
> Could anybody show me a workaround?
> 
> Thanks in advance,
> Tamas Nagy

DMD v0.113

<code>
import std.stdio;

void main()
{
   char[] ns;
   char[] es = "";
   
   writefln("Empty String is '%s'", es);
   writefln("Null String is '%s'", ns);
   writefln("Lit Empty String is '%s'", "");
   ns = null;
   writefln("Lit Null String is '%s'", ns);
}
</code>

Output is ...

Empty String is ''
Null String is ''
Lit Empty String is ''
Lit Null String is ''

-- 
Derek
Melbourne, Australia
February 20, 2005
Re: writef is good, printf is bad
MicroWizard wrote:

> printf is old, bad and ugly. Theoretically it is correct. Let's try writef.
> What should I do, when a char[] is actually empty (ie. null) ?

It was impossible to guess from your sample code,
but "null" defaults to having a type of (void*)

In order for null to work with writef, it *must* have char[] type.
(I think this is a bug in Phobos, as it should accept "null" too ?)

> char[] firstpart="";
> writefln("firstpart=%s",firstpart); //aborts and rewards with this message
> 
> Runtime error:
> firstpart=Error: std.format formatArg

As others shown, that code is working OK. You seem
to show output from another test program ? <aargh>

1.
>     writefln("Null String is '%s'", null);

Null String is 'Error: std.format formatArg

2.
>     writefln("Empty String is '%s'", "");

Empty String is ''

3.
>     writefln("Null String is '%s'", cast(char[]) null);

Null String is ''


But just using "" instead of cast(char[]) null is a lot easier ?
Having a null char[] variable, instead of a literal, works too.

There are a still quite a few quirks in Phobos, regarding usage
of "" versus null. Hopefully they can all be sorted out in time...

--anders
February 21, 2005
Re: writef is good, printf is bad
In article <cvabia$2vst$2@digitaldaemon.com>,
=?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says...
>1.
>>     writefln("Null String is '%s'", null);
>Null String is 'Error: std.format formatArg
>2.
>>     writefln("Empty String is '%s'", "");
>Empty String is ''

You got the point. I was too lazy to copy the code precisely.
The original type was byte[] and not char[] the automatic casting
works (somehow).

>But just using "" instead of cast(char[]) null is a lot easier ?
>Having a null char[] variable, instead of a literal, works too.
>
>There are a still quite a few quirks in Phobos, regarding usage
>of "" versus null. Hopefully they can all be sorted out in time...

Thanks a lot.
I agree, D should be grown up, and these anomalies will be solved.

Tamas Nagy
February 21, 2005
Re: writef is good, printf is bad
MicroWizard wrote:

>>There are a still quite a few quirks in Phobos, regarding usage
>>of "" versus null. Hopefully they can all be sorted out in time...
>> 
> Thanks a lot.
> I agree, D should be grown up, and these anomalies will be solved.

Not a problem. There's another thread going in the bugs group
about the difference (or the lack thereof) between null and ""...

And I do think your subject should be "writef is D, printf is C".
Then again, to some people that seems to mean the same thing :-)

--anders
Top | Discussion index | About this forum | D home