Thread overview
bug in grammar with ?:
Sep 17, 2019
Brett
Sep 17, 2019
ketmar
Sep 17, 2019
Brett
Sep 17, 2019
Dennis
Sep 17, 2019
ketmar
Sep 17, 2019
Adam D. Ruppe
September 17, 2019
Fails(loop is not taken):

for(ulong i = len - 1; i >= (true) ? 0 : 1; i--)


passes

for(ulong i = len - 1; i >= ((true) ? 0 : 1); i--)

Only difference is parenthesis(yes, that is all, doesn't matter if it's true or false)






September 17, 2019
Brett wrote:

> Fails(loop is not taken):
>
> for(ulong i = len - 1; i >= (true) ? 0 : 1; i--)
>
>
> passes
>
> for(ulong i = len - 1; i >= ((true) ? 0 : 1); i--)
>
> Only difference is parenthesis(yes, that is all, doesn't matter if it's true or false)

not a bug. the whole thing before `?` is a condition, so

	i >= (true) ? 0 : 1

returns 0, of course, because `i >= 1` (your `len` is obviously >= 1), and the result of the expression is therefor 0.
September 17, 2019
On Tuesday, 17 September 2019 at 18:03:20 UTC, Brett wrote:
> for(ulong i = len - 1; i >= (true) ? 0 : 1; i--)
> for(ulong i = len - 1; i >= ((true) ? 0 : 1); i--)

Those are two different things by design. The >= binds more tightly than the ?:, it lets you do stuff like

a > 0 ? "greater than" : "not greater than"


Thus the first  one is more like

if(i >= (true)) {
  return 0;
} else {
  return 1; {
}

and the second one is

if(i >= ((true) ? 0 : 1)


Though I personally like to use extra parens though just remember that true and (true) are no different, regardless of context - those parens are totally redundant..
September 17, 2019
On Tuesday, 17 September 2019 at 18:06:55 UTC, ketmar wrote:
> Brett wrote:
>
>> Fails(loop is not taken):
>>
>> for(ulong i = len - 1; i >= (true) ? 0 : 1; i--)
>>
>>
>> passes
>>
>> for(ulong i = len - 1; i >= ((true) ? 0 : 1); i--)
>>
>> Only difference is parenthesis(yes, that is all, doesn't matter if it's true or false)
>
> not a bug. the whole thing before `?` is a condition, so
>
> 	i >= (true) ? 0 : 1
>
> returns 0, of course, because `i >= 1` (your `len` is obviously
> >= 1), and the result of the expression is therefor 0.

Makes sense, seems bad though... can introduce severe bugs in code if allowed. While using ?: there is not such a common idiom, using >= (or <= is)

Might be better for the compiler to detect such things and give a warning to use parenthesis? It could introduce subtle and hard to detect bugs.

Probably 99% of for loops use the syntax of var >= ...

I use it so much that way that I always think of ... as it's own expression rather than the entire part being an expression... it's my fault but I imagine 95% of programmers also think along similar ways. All it takes is forgetting this one time and one could have serious bugs(if the condition is complex and resolves in a hard to detect way it could produce such cases, rare but possible).


September 17, 2019
On Tuesday, 17 September 2019 at 18:16:41 UTC, Brett wrote:
> All it takes is forgetting this one time and one could have serious bugs(if the condition is complex and resolves in a hard to detect way it could produce such cases, rare but possible).

To be honest, your entire loop is one big code smell.

for(ulong i = len - 1; i >= ((true) ? 0 : 1); i--)

if len = 0, i will underflow and be initialized to ulong.max.
i >= 0 is vacuously true for an unsigned integer, resulting in and endless loop

I don't know your original situation, but if you can, please use foreach or foreach_reverse.
September 17, 2019
Brett wrote:

> Makes sense, seems bad though... can introduce severe bugs in code if allowed. While using ?: there is not such a common idiom, using >= (or <= is)
yeah. that's why ternary is not recommended to use -- it is not always obvious what is a condition there. ternary has one of the lowest priorities, and it is right-associative too.