DIP9 -- Redo toString API

Steven Schveighoffer schveiguy at yahoo.com
Fri Nov 19 06:23:16 PST 2010


On Fri, 19 Nov 2010 09:14:07 -0500, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Steven Schveighoffer:
>
>> I've made suggested changes, please review again.
>
> Good, thank you.
>
> Just a note: a DIP is a public document so it's better to encourage good  
> idioms inside it. "null" to represent empty arrays/strings is a bad  
> practice, so instead of this:
>
>    void writeTo(scope delegate(in char[] data) sink, string format =  
> null) const
>    {
>       formattedWrite(sink, "(%s, %s)", first, second);
>    }
>
>
> I suggest you to write something like:
>
>    void writeTo(scope delegate(in char[] data) sink, string format="")  
> const
>    {
>       formattedWrite(sink, "(%s, %s)", first, second);
>    }

Who said setting an array to null is bad practice?  I disagree.  null is a  
better way to represent it, because it sets both the ptr and the length to  
0, while "" only sets the length to 0.

char[] arr = "";

assert(arr == null); // passes
assert(arr is null); // fails

I'd rather use a value that also allows code like:

if(format is null)
{
    // default to decimal
    format = "d";
}

-Steve


More information about the Digitalmars-d mailing list