Jump to page: 1 215  
Page
Thread overview
if(arr) now a warning
Apr 09, 2015
bearophile
Apr 09, 2015
H. S. Teoh
Apr 10, 2015
Daniel Murphy
Apr 10, 2015
H. S. Teoh
Apr 10, 2015
Temtaime
Apr 10, 2015
Daniel Murphy
Apr 10, 2015
Daniel Kozak
Apr 10, 2015
Jacob Carlborg
Apr 10, 2015
biozic
Apr 10, 2015
John Colvin
Apr 10, 2015
JN
Apr 10, 2015
w0rp
Apr 10, 2015
Martin Nowak
Apr 10, 2015
Daniel Murphy
Apr 10, 2015
Martin Krejcirik
Apr 10, 2015
bearophile
Apr 10, 2015
Namespace
Apr 10, 2015
Jacob Carlborg
Apr 10, 2015
Meta
Apr 11, 2015
w0rp
Apr 11, 2015
Martin Nowak
Apr 10, 2015
Martin Nowak
Apr 10, 2015
w0rp
Apr 10, 2015
Andre Kostur
Apr 10, 2015
Daniel Murphy
Apr 10, 2015
ketmar
Apr 10, 2015
Daniel Murphy
Apr 10, 2015
ketmar
Apr 10, 2015
w0rp
May 04, 2015
ketmar
Apr 19, 2015
Jonathan M Davis
Apr 19, 2015
Marc Schütz
Apr 20, 2015
Kagamin
Apr 20, 2015
Jonathan M Davis
Apr 22, 2015
Kagamin
Apr 22, 2015
Jonathan M Davis
Apr 23, 2015
Kagamin
Apr 23, 2015
w0rp
Apr 10, 2015
John Colvin
Apr 10, 2015
Daniel Kozák
Apr 10, 2015
Kagamin
Apr 10, 2015
jkpl
Apr 10, 2015
Marc Schütz
Apr 10, 2015
Daniel Murphy
Apr 10, 2015
deadalnix
Apr 10, 2015
Vladimir Panteleev
Apr 10, 2015
Kagamin
Apr 10, 2015
Daniel Murphy
Apr 10, 2015
John Colvin
Apr 10, 2015
John Colvin
Apr 10, 2015
ketmar
Apr 10, 2015
ketmar
Apr 22, 2015
Martin Nowak
Apr 29, 2015
bearophile
Apr 29, 2015
Iain Buclaw
Apr 30, 2015
Iain Buclaw
Apr 29, 2015
Marc Schütz
Apr 29, 2015
Vladimir Panteleev
Apr 29, 2015
Marc Schütz
Apr 29, 2015
Jonathan M Davis
Apr 30, 2015
Martin Nowak
Apr 30, 2015
Jonathan M Davis
Apr 30, 2015
Byron Heads
Apr 30, 2015
Vladimir Panteleev
Apr 30, 2015
Kagamin
Apr 30, 2015
deadalnix
Apr 30, 2015
Daniel Murphy
Apr 30, 2015
Vladimir Panteleev
Apr 30, 2015
Daniel Murphy
Apr 30, 2015
Iain Buclaw
Apr 30, 2015
Daniel Murphy
May 01, 2015
Daniel Murphy
May 01, 2015
deadalnix
May 01, 2015
Vladimir Panteleev
May 01, 2015
deadalnix
May 01, 2015
Vladimir Panteleev
May 01, 2015
ketmar
May 01, 2015
deadalnix
May 01, 2015
Walter Bright
May 01, 2015
deadalnix
May 01, 2015
Walter Bright
May 02, 2015
Martin Nowak
May 02, 2015
Walter Bright
May 02, 2015
weaselcat
May 03, 2015
ketmar
May 02, 2015
deadalnix
May 02, 2015
Walter Bright
Apr 30, 2015
deadalnix
Apr 30, 2015
Jeremy Powers
Apr 30, 2015
Vladimir Panteleev
Apr 30, 2015
deadalnix
Apr 30, 2015
TC
May 01, 2015
ketmar
Apr 30, 2015
Brian Schott
Apr 30, 2015
deadalnix
May 01, 2015
Vladimir Panteleev
May 01, 2015
Vladimir Panteleev
Apr 30, 2015
Daniel Kozak
May 01, 2015
deadalnix
May 01, 2015
Daniel Kozak
May 01, 2015
Paolo Invernizzi
May 01, 2015
Jonathan M Davis
May 01, 2015
Vladimir Panteleev
May 01, 2015
Walter Bright
May 01, 2015
ketmar
May 01, 2015
deadalnix
May 02, 2015
Jonathan M Davis
May 02, 2015
Vladimir Panteleev
May 02, 2015
Walter Bright
May 01, 2015
Iain Buclaw
May 01, 2015
Paolo Invernizzi
May 01, 2015
Iain Buclaw
May 01, 2015
Jonathan M Davis
Apr 30, 2015
Kagamin
Apr 30, 2015
bearophile
Apr 29, 2015
deadalnix
April 09, 2015
A confusing and dangerous "feature" of D was to treat the expression if(arr) as meaning if(arr.ptr).

Anyone coming from any number of other languages may think that if(arr) means "if arr has any elements". Typically one cares what is inside an array, not where it lives.

However, while checking to see if an array has elements is very similar to checking for a null pointer (obviously, a null pointer array should have no elements), it's not equivalent. Therefore, when attempting to fix this confusion, we had to disable the if(arr) check altogether. You must now specify what part of the array you care about, if(arr.ptr) or if(arr.length).

As usual with changes of this nature, there are differing opinions, and differing styles. I personally never use null as a special value for an array, but others do. They use if(arr) to actually mean if(arr.ptr). For these cases, the update will cause some amount of busywork changing these checks.

But when updating Phobos to comply with this change, we found several actual incorrect usages. So I think the change is worth it, and not much busywork. YMMV.

What do you think?

references:

https://issues.dlang.org/show_bug.cgi?id=4733
https://github.com/D-Programming-Language/dmd/pull/2885
https://github.com/D-Programming-Language/tools/pull/166

-Steve
April 09, 2015
Steven Schveighoffer:

> What do you think?

I asked for this fix almost five years ago, so I think it's about time :-)

Bye,
bearophile
April 09, 2015
On Thu, Apr 09, 2015 at 04:59:00PM -0400, Steven Schveighoffer via Digitalmars-d wrote:
> A confusing and dangerous "feature" of D was to treat the expression
> if(arr) as meaning if(arr.ptr).
[...]
> Therefore, when attempting to fix this confusion, we had to disable
> the if(arr) check altogether. You must now specify what part of the
> array you care about, if(arr.ptr) or if(arr.length).
[...]
> But when updating Phobos to comply with this change, we found several actual incorrect usages. So I think the change is worth it, and not much busywork.  YMMV.
[...]

I agree it's worth it. It's one of the very legitimate reasons for breaking code IMO. Or rather, to bring to the user's attention code that likely already contains latent bugs.

Sure some people will complain about it, but in the long run, I think they will come to appreciate the improved code correctness that this confers.

It's akin to a similar change sometime ago that made bare pointers in if-conditions illegal: writing `if (ptr && ...)` will cause the compiler to complain loudly. When this first got in, one of my projects broke, and I was rather annoyed at the time. However, when I got around to fixing it, I found that I had to write `if (ptr !is null && ...)` instead, which greatly clarifies the intent and readability of the code. Since then, I have come to appreciate this change.

So this gets a thumbs up from me.


T

-- 
The computer is only a tool. Unfortunately, so is the user. -- Armaphine, K5
April 10, 2015
"H. S. Teoh via Digitalmars-d"  wrote in message news:mailman.1388.1428620346.3111.digitalmars-d@puremagic.com...

> It's akin to a similar change sometime ago that made bare pointers in
> if-conditions illegal: writing `if (ptr && ...)` will cause the compiler
> to complain loudly. When this first got in, one of my projects broke,
> and I was rather annoyed at the time. However, when I got around to
> fixing it, I found that I had to write `if (ptr !is null && ...)`
> instead, which greatly clarifies the intent and readability of the code.
> Since then, I have come to appreciate this change.

Except that never happened.  Maybe you're thinking of comparing objects to null with ==? 

April 10, 2015
On Fri, Apr 10, 2015 at 01:01:48PM +1000, Daniel Murphy via Digitalmars-d wrote:
> "H. S. Teoh via Digitalmars-d"  wrote in message news:mailman.1388.1428620346.3111.digitalmars-d@puremagic.com...
> 
> >It's akin to a similar change sometime ago that made bare pointers in if-conditions illegal: writing `if (ptr && ...)` will cause the compiler to complain loudly. When this first got in, one of my projects broke, and I was rather annoyed at the time. However, when I got around to fixing it, I found that I had to write `if (ptr !is null && ...)` instead, which greatly clarifies the intent and readability of the code.  Since then, I have come to appreciate this change.
> 
> Except that never happened.  Maybe you're thinking of comparing objects to null with ==?

Are you sure?

My original failing code was:

	while (!lex.empty &&
		(op = T.matchOp(lex.front, OpType.infix)) &&
		op.precedence >= minprec)
	{
		...
	}

where op is a pointer type.


T

-- 
In a world without fences, who needs Windows and Gates? -- Christian Surchi
April 10, 2015
Using an array type in boolean contexts is bug-prone.
Please just write « arr.length » of anything you want.
If you want to see a reasons to forbid it - look at original bug report.
April 10, 2015
"H. S. Teoh via Digitalmars-d"  wrote in message news:mailman.1397.1428640119.3111.digitalmars-d@puremagic.com...

> Are you sure?

Pretty sure.

> My original failing code was:
>
> while (!lex.empty &&
> (op = T.matchOp(lex.front, OpType.infix)) &&
> op.precedence >= minprec)
> {
> ...
> }
>
> where op is a pointer type.

Maybe an 'assignment cannot be used as a condition' error?  Nothing to do with pointer types as boolean conditions.

eg if (x = call()) -> if ((x = call()) != null)

Do you have a complete example that gives the error you're talking about? 

April 10, 2015
On Thursday, 9 April 2015 at 20:59:00 UTC, Steven Schveighoffer wrote:
> A confusing and dangerous "feature" of D was to treat the expression if(arr) as meaning if(arr.ptr).
>
> Anyone coming from any number of other languages may think that if(arr) means "if arr has any elements". Typically one cares what is inside an array, not where it lives.
>
> However, while checking to see if an array has elements is very similar to checking for a null pointer (obviously, a null pointer array should have no elements), it's not equivalent. Therefore, when attempting to fix this confusion, we had to disable the if(arr) check altogether. You must now specify what part of the array you care about, if(arr.ptr) or if(arr.length).
>
> As usual with changes of this nature, there are differing opinions, and differing styles. I personally never use null as a special value for an array, but others do. They use if(arr) to actually mean if(arr.ptr). For these cases, the update will cause some amount of busywork changing these checks.
>
> But when updating Phobos to comply with this change, we found several actual incorrect usages. So I think the change is worth it, and not much busywork. YMMV.
>
> What do you think?
>
> references:
>
> https://issues.dlang.org/show_bug.cgi?id=4733
> https://github.com/D-Programming-Language/dmd/pull/2885
> https://github.com/D-Programming-Language/tools/pull/166
>
> -Steve

This change meens I had to rewrite almost 1k lines of code which almost every one has been wrong :D.

I have lot of code rewriten from php directly to D.

PHP:
http://sandbox.onlinephpfunctions.com/code/b75620f9714784bd888b365b963723a1dc773d08

D:
http://dpaste.dzfl.pl/89f762890526
April 10, 2015
On 2015-04-09 22:59, Steven Schveighoffer wrote:

> As usual with changes of this nature, there are differing opinions, and
> differing styles. I personally never use null as a special value for an
> array, but others do. They use if(arr) to actually mean if(arr.ptr). For
> these cases, the update will cause some amount of busywork changing
> these checks.
>
> But when updating Phobos to comply with this change, we found several
> actual incorrect usages. So I think the change is worth it, and not much
> busywork. YMMV.
>
> What do you think?

I'm wondering how often in practice .ptr is not null but the array is empty.

-- 
/Jacob Carlborg
April 10, 2015
On Friday, 10 April 2015 at 08:38:00 UTC, Jacob Carlborg wrote:
> On 2015-04-09 22:59, Steven Schveighoffer wrote:
>
>> As usual with changes of this nature, there are differing opinions, and
>> differing styles. I personally never use null as a special value for an
>> array, but others do. They use if(arr) to actually mean if(arr.ptr). For
>> these cases, the update will cause some amount of busywork changing
>> these checks.
>>
>> But when updating Phobos to comply with this change, we found several
>> actual incorrect usages. So I think the change is worth it, and not much
>> busywork. YMMV.
>>
>> What do you think?
>
> I'm wondering how often in practice .ptr is not null but the array is empty.

When you remove elements from arrays: you end up with slices of length 0 that have a non-null .ptr. Not that rare.
« First   ‹ Prev
1 2 3 4 5 6 7 8 9 10 11