D as a prototyping language (for C/C++ projects)

Jonathan M Davis jmdavisProg at gmx.com
Thu Feb 28 00:16:18 PST 2013


On Thursday, February 28, 2013 09:05:19 Jacob Carlborg wrote:
> On 2013-02-28 04:35, Rob T wrote:
> > I had a look at your isBlank function. Why not check if length exists
> > and run it as follows?
> > 
> > @property bool isBlank (T) (T t)
> > {
> > 
> >     static if (__traits(compiles, t.length))
> >     {
> >     
> >         if (t.length == 0)
> >         
> >            return true;
> >     
> >     }
> > 
> > ...
> > 
> > That would be more generic I think.
> 
> Sure, I could do that. But I thought checking for "empty" would be more
> generic. I would expect every type having "length" would/should also
> have "empty" but perhaps that's a wrong assumption.

All ranges are guaranteed to have empty, so any range with length will 
obviously have empty. Regardless, in general, it's better to check for empty 
than length, since it's possible for empty to be more efficient, and it's 
unlikely the empty will ever be less efficient than checking that length == 0. 
If you're not talking about ranges, then all bets are off as to what it does 
and doesn't have, because that would depend on what APIs it adheres to and 
what they're like.

- Jonathan M Davis


More information about the Digitalmars-d mailing list