February 22, 2012 Re: empty arrays and cast(bool): WAT | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Murphy | On Sun, 19 Feb 2012 06:06:19 -0000, Daniel Murphy <yebblies@nospamgmail.com> wrote:
> I agree, and I doubt it was intentional.
>
> if (arr) should mean if (arr.length && arr.ptr)
> But since you can only get (arr.length != 0 && arr.ptr == null) when doing
> unsafe things with arrays, I think it's entirely reasonable to use
> if (arr) -> if (arr.length)
>
> This is what I expected.
Just to provide a counterpoint, I would have expected if (arr) to check for null. Coming from a C background I expect the thing inside () to be compared to 0 and 'the thing' in my mind is the array itself, not any property of the array length included. Not saying checking for null is a better or worse idea, just saying that would be my initial impression of what it would do. I too am unsettled by the conflation of null and empty - and argued against it on several occasions, but hey, too late now I suspect.
Regan
|
February 22, 2012 Re: empty arrays and cast(bool): WAT | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath:
> I too am unsettled by the conflation of null and empty - and argued against it on several occasions, but hey, too late now I suspect.
It's not too much late.
Bye,
bearophile
|
February 22, 2012 Re: empty arrays and cast(bool): WAT | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Wed, Feb 22, 2012 at 01:13:24PM -0500, bearophile wrote: > Regan Heath: > > > I too am unsettled by the conflation of null and empty - and argued against it on several occasions, but hey, too late now I suspect. > > It's not too much late. [...] I agree. Conflating null and empty has been (one of) the source(s) of stupidities like the === operator in Javascript and PHP. I mean, c'mon. Will the next generation of languages start introducing the ==== operator now? We already have .empty, why should the language go out of its way to let you test an array with "if(array){...}"? Code like that is hard to read and fails to convey intent clearly. "if (array.empty)" and "if (array is null)" are much more self-documenting and less prone to misinterpretation. T -- "How are you doing?" "Doing what?" |
March 05, 2012 Re: empty arrays and cast(bool): WAT | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Sat, 18 Feb 2012 21:01:55 -0500, Timon Gehr <timon.gehr@gmx.ch> wrote:
> Why does the following code behave funny?
>
> void main(){
> string x = " "[1..1];
> writeln(cast(bool)x); // true
> char[] y = [' '][1..1];
> writeln(cast(bool)y); // false
> }
>
> Is there any reason for empty arrays to evaluate to true? This is very bug prone.
Just to weigh in:
1. The most intuitive and useful thing is to check for arr.length.
2. Given that there are valid cases for checking null vs. empty, I think there should be a way to find usages of if(arr) for fixing legacy code.
To disallow if(arr) simply because of legacy code is a step in the *wrong* direction. The experience with arrays is going to be a major contributing factor to the enjoyment of using D. I'd rather see the compiler show you where cases of if(arr) appear, and allow you to judge whether those should be arr.ptr or not.
I agree with Timon, let's make it more intuitive, and break code, and let those people be able to fix that code somehow. If this means deprecating it for a time, so be it.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation