July 09, 2017
On Sunday, 9 July 2017 at 15:10:56 UTC, ag0aep6g wrote:
> On 07/09/2017 03:51 PM, kdevel wrote:
>> On Sunday, 9 July 2017 at 10:32:23 UTC, ag0aep6g wrote:

[...]

> A null char* is not a proper C string.

A C string is a sequence of storage units containing legitimate character values of which the last one is the NUL character.

> It doesn't have length 0. It has no length.

In C a NULL ptr does not refer to anything including not to a C string.

> A C function can't return a null char* when it's supposed to return an empty string.

That is true. And this it what mislead me thinking that D behaves the same.

> But a D function can return a null char[] in that case.

Yes, it can obviously return one of the two representations of the empty D string.

Stefan
July 09, 2017
On Sunday, 9 July 2017 at 18:55:51 UTC, kdevel wrote:
> Yes, it can obviously return one of the two representations of the empty D string.

The two being null and ""? There are more than those. One for every possible .ptr value.
July 09, 2017
On Sunday, July 9, 2017 1:51:44 PM MDT kdevel via Digitalmars-d-learn wrote:
> > You also shouldn't rely on it returning null for a null input, even when it currently does that.
>
> I assumed that a non-null string is returned for a non-null input.

There are going to be functions that return null rather than an empty slice of the original array. You really can't rely on getting an empty array instead of a null one from a function unless the documentation tells you that. For most purposes, there is no practical difference between a null array and an empty array, so very little code is written which cares about the difference. The only place where I would expect a function in a library to distinguish is if its documentation says that it does (e.g. if returning null means something specific or if it specifically says that the result is a slice of the input).

In general, relying on whether a dynamic array is null or not outside of code that you control or functions that are explicit about what they so with null is risky business.

Sometimes, I wish that null were not treated as empty, and you were forced to allocate a new array or somesuch rather than having null arrays just work - then you could actually rely on stuff being null or not - but that would also result in a lot more segfaults when people screwed up. The status quo works surprisingly well overall. It just makes it dangerous to do much with distinguishing null arrays from empty ones.

- Jonathan M Davis

1 2
Next ›   Last »