Jump to page: 1 24  
Page
Thread overview
null == "" is true?
Jul 12, 2022
Antonio
Jul 12, 2022
H. S. Teoh
Jul 12, 2022
Hipreme
Jul 12, 2022
Paul Backus
Jul 12, 2022
Antonio
Jul 12, 2022
Antonio
Jul 15, 2022
Salih Dincer
Jul 15, 2022
Salih Dincer
Jul 18, 2022
Kagamin
Jul 18, 2022
Antonio
Jul 19, 2022
Kagamin
Jul 19, 2022
Antonio
Jul 19, 2022
Kagamin
Jul 20, 2022
Antonio
Jul 19, 2022
Kagamin
Jul 19, 2022
Antonio
Jul 20, 2022
Kagamin
Jul 20, 2022
Antonio
Jul 12, 2022
user1234
Jul 12, 2022
ag0aep6g
Jul 12, 2022
H. S. Teoh
Jul 12, 2022
ag0aep6g
Jul 12, 2022
user1234
Jul 19, 2022
Bienlein
Jul 12, 2022
Ali Çehreli
Jul 15, 2022
Palak
July 12, 2022

It works

void main()
{
   assert(null=="");
}

why?

July 12, 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"
July 12, 2022
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

On 7/12/22 12:27 PM, Antonio wrote:

>

It works

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

July 12, 2022

On 7/12/22 12:40 PM, H. S. Teoh wrote:

>

Because an empty string is, by default, represented by an empty slice of
the null pointer.

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
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

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 == operator because two empty strings always compare equal with ==, regardless of what they point to.

string s = "hello";
string empty1 = s[0 .. 0];
string empty2 = s[1 .. 1];
assert(empty1 == null);
assert(empty2 == null);
assert(empty1 == empty2);

The real problem is that s == null looks like it does one thing (test for a null pointer) while actually doing something slightly different (test for an empty string).

July 12, 2022

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

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;

Absolutely. I'd like to add: especially as default parameter value that's an array. Never use null. use [] (empty array literal).

July 12, 2022

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.

July 12, 2022
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
« First   ‹ Prev
1 2 3 4