Thread overview
What is the correct way to test for an empty string?
Jul 16, 2013
Gary Willoughby
Jul 16, 2013
Adam D. Ruppe
Jul 16, 2013
bearophile
Jul 16, 2013
Gary Willoughby
Jul 17, 2013
Rob T
Jul 17, 2013
Jesse Phillips
Jul 17, 2013
H. S. Teoh
July 16, 2013
What is the correct way to test for an empty string?

I've used

if (string == "")

and

if (string is null)

and both (O_o) in some places, it's starting to do my head in. What is the correct way?
July 16, 2013
I just use

if(string.length == 0) {}

which covers both cases and is pretty intuitive too.
July 16, 2013
Gary Willoughby:

> What is the correct way to test for an empty string?
>
> I've used
>
> if (string == "")
>
> and
>
> if (string is null)
>
> and both (O_o) in some places, it's starting to do my head in. What is the correct way?

The right, safe and readable way is to use std.array.empty:

if (myString.empty)

If you don't want to import functions, then test for the length:

if (string.length == 0)

Bye,
bearophile
July 16, 2013
On Tuesday, 16 July 2013 at 19:33:13 UTC, bearophile wrote:
> The right, safe and readable way is to use std.array.empty:
>
> if (myString.empty)

OMG, of course. Thanks!

July 17, 2013
On Tuesday, 16 July 2013 at 19:33:13 UTC, bearophile wrote:
>
> The right, safe and readable way is to use std.array.empty:
>
> if (myString.empty)
>
> If you don't want to import functions, then test for the length:
>
> if (string.length == 0)
>
> Bye,
> bearophile

What was the rational for empty not being built in?

Is there a performance penalty using "empty"?

--rt
July 17, 2013
On Wednesday, 17 July 2013 at 19:18:29 UTC, Rob T wrote:
> What was the rational for empty not being built in?
>
> Is there a performance penalty using "empty"?
>
> --rt

empty() was added to provide a range interface to arrays. Probably wasn't built in since you have to handle all array types. The concept of ranges make this clear, and since it didn't need to be built in to make it a range, it wasn't.
July 17, 2013
On Wed, Jul 17, 2013 at 09:18:27PM +0200, Rob T wrote:
> On Tuesday, 16 July 2013 at 19:33:13 UTC, bearophile wrote:
> >
> >The right, safe and readable way is to use std.array.empty:
> >
> >if (myString.empty)
> >
> >If you don't want to import functions, then test for the length:
> >
> >if (string.length == 0)
> >
> >Bye,
> >bearophile
> 
> What was the rational for empty not being built in?
> 
> Is there a performance penalty using "empty"?
[...]

AFAICT the reason is mainly historical. The whole deal with ranges was introduced rather late into the language, whereas array.length has always been there as a built-in feature. So .empty was layered on top of the language primitives after the fact, that's why it's in std.array instead of being built-in to the language.

In a sense, it's a good thing to have only .length built-in; it simplifies the core language and the compiler, and allows more compile-time flexibility. OTOH, though, it might be a good idea to move parts of std.array into druntime's object.di so that things like .empty are available by default, rather than hiding in an obscure corner of Phobos. If ranges are such a big deal in D, as Walter seems to be pushing for, it makes sense to provide it by default in object.di.


T

-- 
Кто везде - тот нигде.