null == "" is true?
H. S. Teoh
hsteoh at qfbox.info
Tue Jul 12 16:40:38 UTC 2022
On Tue, Jul 12, 2022 at 04:27:44PM +0000, Antonio via Digitalmars-d-learn wrote:
> It works
>
> ```d
> void main()
> {
> assert(null=="");
> }
> ```
>
> why?
Because an empty string is, by default, represented by an empty slice of
the null pointer.
Do not rely on this, however; it's possible sometimes to get an empty
string that isn't null, e.g., if you incrementally shrink a slice over a
string until it's empty. In that case, .ptr will not be null, but the
string will still be empty. Always compare strings against "" rather
than null, because the latter may not do what you think it does
sometimes.
T
--
One reason that few people are aware there are programs running the internet is that they never crash in any significant way: the free software underlying the internet is reliable to the point of invisibility. -- Glyn Moody, from the article "Giving it all away"
More information about the Digitalmars-d-learn
mailing list