null == "" is true?
Steven Schveighoffer
schveiguy at gmail.com
Tue Jul 12 17:11:59 UTC 2022
On 7/12/22 12:27 PM, Antonio wrote:
> It works
>
> ```d
> void main()
> {
> assert(null=="");
> }
> ```
>
> why?
A string is not exactly a reference type. It's a length and a pointer.
This can be confusing to newcomers, especially ones that come from
languages that treat arrays and strings as object references.
`null` as an array with `0` length and `null` pointer.
`""` is an array with `0` length and a pointer to a zero character (not
`null`).
The algorithm to compare *any* arrays is first verify the lengths are
the same. Then for each element in the array, compare them. Since there
are 0 elements in both the empty string and the null string, they are equal.
-Steve
More information about the Digitalmars-d-learn
mailing list