const assignments problem again
Jonathan M Davis
jmdavisProg at gmx.com
Sun Aug 7 02:03:38 PDT 2011
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 problem, you can as well read wrong variable or call wrong function.
> No language will help you if your code is junk. You should fix this in a
> different way.
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 could accidentally end up copying the target to
the source, and it may or may not be an easy bug to catch. I made that exact
mistake in C++ with its copy function just the other day. const saved me a lot
of headaches.
Now, I think that D gives us enough ways to deal with the problem that
Bearophile illustrates here (and Bearophile actually showed us a number of
ways that D allows us to do what he's trying to do), so I don't think that we
really need to do anything to the language to better deal with this situation.
But accidental overwrites _can_ be a problem, and that's one of the things
that const catches. So, not being able to use const when you should logically
be able to due to syntax problems in the language would definitely be a problem
- not the biggest problem ever perhaps, but it _would_ be a problem.
Fortunately however, D gives us plenty of ways to get around the problem.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list