It works
void main()
{
assert(null=="");
}
why?
Thread overview | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 12, 2022 null == "" is true? | ||||
---|---|---|---|---|
| ||||
It works
why? |
July 12, 2022 Re: null == "" is true? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Antonio | 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" |
July 12, 2022 Re: null == "" is true? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote:
> 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
Yup, always compare the string with "". I have had this kind of problem a bunch of times, comparing it with null but it is not actually null
|
July 12, 2022 Re: null == "" is true? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Antonio | On 7/12/22 12:27 PM, Antonio wrote: >It works
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.
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 |
July 12, 2022 Re: null == "" is true? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On 7/12/22 12:40 PM, H. S. Teoh wrote: >Because an empty string is, by default, represented by an empty slice of No, it's not a null pointer. It's a pointer to a zero-character. But it is indeed an empty slice. -Steve |
July 12, 2022 Re: null == "" is true? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 7/12/22 10:11, Steven Schveighoffer wrote: > 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. Checking .empty() covered all of my uses cases. I think... :) void foo(string s) { import std.array; assert(s.empty); } void main() { // Literal null foo(null); // Zero-length and pointing at '\0' foo(""); // Fresh string a; foo(a); // Shrunk string b = "hello"; b.length = 0; assert(b.ptr !is null); foo(b); } Ali |
July 12, 2022 Re: null == "" is true? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote: >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. This is actually 100% reliable when comparing with the
The real problem is that |
July 12, 2022 Re: null == "" is true? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote: >On Tue, Jul 12, 2022 at 04:27:44PM +0000, Antonio via Digitalmars-d-learn wrote: >It works
why? Because an empty string is, by default, represented by an empty slice of the null pointer. Do not rely on this, however; Absolutely. I'd like to add: especially as default parameter value that's an array. Never use null. use |
July 12, 2022 Re: null == "" is true? | ||||
---|---|---|---|---|
| ||||
Posted in reply to user1234 | On Tuesday, 12 July 2022 at 19:02:01 UTC, user1234 wrote: >On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote: Do not rely on this, however; Absolutely. I'd like to add: especially as default parameter value that's an array. Never use null. use Just to be clear: |
July 12, 2022 Re: null == "" is true? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Tue, Jul 12, 2022 at 07:55:46PM +0000, ag0aep6g via Digitalmars-d-learn wrote: > On Tuesday, 12 July 2022 at 19:02:01 UTC, user1234 wrote: > > On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote: > [...] > > > Do not rely on this, however; > > > > Absolutely. I'd like to add: especially as default parameter value that's an array. Never use null. use `[]` (empty array literal). > > Just to be clear: `[]` and `null` are the exact same thing (null pointer, zero length). The reason to prefer `[]` over `null` is purely for readability. The meaning is exactly the same. Pedantically, no, they're not the same. You can assign null to a pointer, but you can't assign [] to a pointer. `null` is a supertype of `[]`. But probably nobody actually cares about this distinction. :-D T -- By understanding a machine-oriented language, the programmer will tend to use a much more efficient method; it is much closer to reality. -- D. Knuth |