Re: const assignments problem again

2011-08-20 Thread bearophile
Kagamin: > P.S. you would make a better point if it were a const instance of a class. You can't even define V as "const struct" and use it like this: const struct V { double x, y; } // wrong void main() { double x = 1, y = 2; int c = 1; V v; switch (c) { case 1:

Re: const assignments problem again

2011-08-09 Thread Jacob Carlborg
On 2011-08-09 07:04, Don wrote: Jacob Carlborg wrote: On 2011-08-07 03:19, bearophile wrote: I have discussed about this topic once in past, but in the meantime I have seen this is a quite common problem, so I think it doesn't harm to touch this topic again. This is a direct D translation of t

Re: const assignments problem again

2011-08-08 Thread Don
Jacob Carlborg wrote: On 2011-08-07 03:19, bearophile wrote: I have discussed about this topic once in past, but in the meantime I have seen this is a quite common problem, so I think it doesn't harm to touch this topic again. This is a direct D translation of the original C or C++ code: //

Re: const assignments problem again

2011-08-07 Thread Jacob Carlborg
On 2011-08-07 03:19, bearophile wrote: I have discussed about this topic once in past, but in the meantime I have seen this is a quite common problem, so I think it doesn't harm to touch this topic again. This is a direct D translation of the original C or C++ code: // version #1 double foo;

Re: const assignments problem again

2011-08-07 Thread Lutger Blijdestijn
Timon Gehr wrote: > Lutger Blijdestin wrote: >> I like the ternary operator the best for this, as it is the simplest. I >> always write them like this though, liberally include parenthesis and >> never nest: >> (condition) >> ? (truth value) >> : (false value) > > What is the benefit of

Re: const assignments problem again

2011-08-07 Thread Timon Gehr
Lutger Blijdestin wrote: > I like the ternary operator the best for this, as it is the simplest. I > always write them like this though, liberally include parenthesis and never > nest: > (condition) > ? (truth value) > : (false value) What is the benefit of this, compared to leaving paren

Re: const assignments problem again

2011-08-07 Thread Lutger Blijdestijn
bearophile wrote: ... > In my precedent post about this topic I have discussed "nested constness" > and another partially silly idea. Recently I have seen a language that > suggests me this: > > // version #6 > const foo; > if (abs(e.x - v.x) > double.min) > foo = (v.y - e.y) / (v.x - e.x); >

Re: const assignments problem again

2011-08-07 Thread Jonathan M Davis
On Sunday 07 August 2011 06:02:36 Kagamin wrote: > Jonathan M Davis Wrote: > > Being able to use const can be very valuable. For instance, what if you > > were using std.algorithm.copy and got the arguments backwards? If the > > source is const, then the compiler will complain, and you'll quickly >

Re: const assignments problem again

2011-08-07 Thread Kagamin
Jonathan M Davis Wrote: > Being able to use const can be very valuable. For instance, what if you were > using std.algorithm.copy and got the arguments backwards? If the source is > const, then the compiler will complain, and you'll quickly find the bug. If > the > source isn't const, then you

Re: const assignments problem again

2011-08-07 Thread Kagamin
bearophile Wrote: > - You can't use "auto" there, so if the type of e.x changes, you have to > change the foo type manually (double.min is replaceable with typeof(e.x).min). This is a bad idea too. The code assumes the values are double. If this assumption is not true, the code is broken.

Re: const assignments problem again

2011-08-07 Thread Jonathan M Davis
On Sunday 07 August 2011 04:40:52 Kagamin wrote: > bearophile Wrote: > > - And foo can't be const or immutable, I don't like this. > > I suppose accidental overwrite bugs are overrated. I have never seen them > even from evil programmers. If you write random code, overwrite is not your > only prob

Re: const assignments problem again

2011-08-07 Thread Kagamin
bearophile Wrote: > - And foo can't be const or immutable, I don't like this. I suppose accidental overwrite bugs are overrated. I have never seen them even from evil programmers. If you write random code, overwrite is not your only problem, you can as well read wrong variable or call wrong fun

Re: const assignments problem again

2011-08-07 Thread Kagamin
P.S. you would make a better point if it were a const instance of a class.

Re: const assignments problem again

2011-08-07 Thread Marco Leise
Am 07.08.2011, 06:31 Uhr, schrieb Adam D. Ruppe : bearophile: I agree, that's why I have added two more points The precedence of the ternary is pretty easy to handle - if there's more than one thing in there, just use parenthesis. That's the only thing I can think of that would make it any

Re: const assignments problem again

2011-08-06 Thread Adam D. Ruppe
bearophile: > I agree, that's why I have added two more points The precedence of the ternary is pretty easy to handle - if there's more than one thing in there, just use parenthesis. That's the only thing I can think of that would make it any more bug prone than other branches. Perhaps it'd be wo

Re: const assignments problem again

2011-08-06 Thread bearophile
Adam D. Ruppe: > > I don't like it. > > That's not good enough. I agree, that's why I have added two more points: - conditional expression is bug-prone and it's a bit tricky (it's a common source of various bugs), because of it operator precedence too; - It is less maintainable (if you want to

Re: const assignments problem again

2011-08-06 Thread Adam D. Ruppe
Reading this post, I kept thinking "why aren't you just using the ternary operator?" > I don't like it. That's not good enough.

const assignments problem again

2011-08-06 Thread bearophile
I have discussed about this topic once in past, but in the meantime I have seen this is a quite common problem, so I think it doesn't harm to touch this topic again. This is a direct D translation of the original C or C++ code: // version #1 double foo; if (abs(e.x - v.x) > double.min) foo