What is the correct way to test for an empty string?

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Jul 17 12:44:32 PDT 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

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


More information about the Digitalmars-d-learn mailing list