std.string.chomp error
Lars T. Kyllingstad
public at kyllingen.NOSPAMnet
Tue Aug 10 00:30:37 PDT 2010
On Mon, 09 Aug 2010 17:35:56 -0700, Jonathan M Davis wrote:
> On Monday, August 09, 2010 17:09:03 simendsjo wrote:
>> On 10.08.2010 02:09, Jonathan M Davis wrote:
>> > On Monday, August 09, 2010 16:59:07 bearophile wrote:
>> >> simendsjo:
>> >>> Ahem.. :) Yes, I did miss your answer! How I got fooled by the
>> >>> preview pane and never noticed the scrollbar.
>> >>
>> >> No problem, it happens, don't worry.
>> >>
>> >>> I cannot see how your other bug report relates to this though.
>> >>
>> >> My other bug report is about this line in your code:
>> >> if (delimiter == null)
>> >>
>> >> I don't like it :-)
>> >>
>> >> Bye,
>> >> bearophile
>> >
>> > Why, because it should be
>> >
>> > if(delimiter is null)
>> >
>> >
>> > or just
>> >
>> > if(!delimiter)
>> >
>> >
>> > - Jonathan M Davis
>>
>> Hehe.. You're a bit beyond my D level right now. At least I now know
>> null == false and you can do reference is null :)
>
> IIRC, you're not really supposed to do "delim == null" but rather us
> "delim is null" or shorten to to just "!delim". Why, I don't recall off
> the top of my head, but it might be because "delim == null" would be
> calling Object.opEquals(), and there's no need for that function call
> (though fortunately "delim == null" translates to Object.opEquals(delim,
> null) rather than delim.opEquals(null) which avoids issues where the lhs
> is null and causes it to go boom).
>
> In either case, for null checks, I'd suggest either just using the
> reference by itself or to explictly use "is null" if you want the extra
> clarity.
No, using 'is' won't work. Check this out:
int[] a;
assert (a == null);
assert (a is null);
a = new int[10];
a.length = 0;
assert (a == null);
assert (a !is null);
The thing is, '==' tests whether two arrays are equal, that is, that they
are equally long and that their elements are equal. Any empty array is
equal to null -- in fact, in this context 'null' is just a way of
denoting an empty array that doesn't point to any particular memory block
(i.e. hasn't been initialised yet).
// This is what '==' does
bool mimicEquals(int[] a, int[] b)
{
if (a.length != b.length) return false;
foreach (i; 0 .. a.length) if (a[i] != b[i]) return false;
return true;
}
'is', on the other hand, tests whether two arrays are identical, i.e.
that they have the same length and *refer to the same piece of memory*.
// This is (sort of) what 'is' does
bool mimicIs(int[] a, int[] b)
{
return (a.ptr == b.ptr && a.length == b.length);
}
-Lars
More information about the Digitalmars-d-learn
mailing list