digitalmars.D.learn - What is the correct way to test for an empty string?
- Gary Willoughby (7/7) Jul 16 2013 What is the correct way to test for an empty string?
- Adam D. Ruppe (3/3) Jul 16 2013 I just use
- bearophile (7/14) Jul 16 2013 The right, safe and readable way is to use std.array.empty:
- Gary Willoughby (2/4) Jul 16 2013 OMG, of course. Thanks!
- Rob T (4/10) Jul 17 2013 What was the rational for empty not being built in?
- Jesse Phillips (5/8) Jul 17 2013 empty() was added to provide a range interface to arrays.
- H. S. Teoh (17/33) Jul 17 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?
Jul 16 2013
I just use if(string.length == 0) {} which covers both cases and is pretty intuitive too.
Jul 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
Jul 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!
Jul 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) If you don't want to import functions, then test for the length: if (string.length == 0) Bye, bearophileWhat was the rational for empty not being built in? Is there a performance penalty using "empty"? --rt
Jul 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"? --rtempty() 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.
Jul 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:[...] 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 -- Кто везде - тот нигде.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, bearophileWhat was the rational for empty not being built in? Is there a performance penalty using "empty"?
Jul 17 2013