null Vs [] return arrays

Regan Heath regan at netmail.co.nz
Thu Apr 7 03:26:49 PDT 2011


On Tue, 05 Apr 2011 18:46:06 +0100, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:
> On Tue, 05 Apr 2011 13:24:49 -0400, Regan Heath <regan at netmail.co.nz>  
> wrote:
>
>> On Fri, 01 Apr 2011 18:23:28 +0100, Steven Schveighoffer  
>> <schveiguy at yahoo.com> wrote:
>>>
>>> assert("" !is null); // works on D.  Try it.
>>
>> Yes, but that's because this is a string literal.  It's not useful  
>> where you're getting your input from somewhere else.. like in the other  
>> 2 use cases I mentioned.
>
> But that isn't the same as [].  Basically, if you have an existing  
> array, and you want to create a non-null empty array out of it, a slice  
> of [0..0] always works.
>
> I know you mention it, but I want to draw attention to the original  
> problem, that [] returns a null array.  Other cases where you are not  
> using [] or "" are a separate issue.
>
> All the cases you have brought up involve strings, for which there is a  
> non-null array returned for "".  I still have not yet seen a compelling  
> use case for making [] return non-null.

Ahh.. I see, I really should have renamed the thread title.  I'm not, and  
never was, arguing for [] (specifically) returning non-null.  Sorry.


> Quoting from your message previously (with added comment):
>
>
> 	case 4:
> 		return cast(char[])"".dup;
> 	case 5:
> 		return cast(char[])""[0..0]; // note lack of .dup
> 	}

Drat, not sure what happened there.  My source had the 'dup' when I went  
back to it.  Sorry.

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list