Why D const is annoying

Timon Gehr timon.gehr at gmx.ch
Sat Dec 10 02:00:07 PST 2011


On 12/10/2011 10:37 AM, Mehrdad wrote:
> I just thought I'd give D another try, after having given up on it for a
> while.
>
> Lo and behold... the same old kind of problem from a year ago is still
> here. :(
>
> Simple stuff like this:
>
> import std.algorithm;
> void main() {
> const arr = [1, 2, 3];
> reduce!"a*b"(arr); // You'd think it'd work...
> }
>
> Results in ridiculously annoying errors like:
> // algorithm.d(728): Error: can only initialize const member
> _field_field_0 inside constructor
>
> I **HIGHLY** suggest that priority be given to simple problems like
> these, instead of OMG-so-cool-libraries.

It is a *library bug*. Just have a look at the source code.

> It really doesn't matter if
> there is an uber-awesome collections/CURL/whatever library out there,
> when problems like these exist. If a 2-line piece of code needs a
> workaround, then (IMO) people simply won't care about anything else
> that's more complicated.
>
> I'll post more as I find them. (I'd found plenty a few months ago, now I
> just need to find them again.)
> Too lazy/busy studying to post as a bug. And it feels a bit better when
> I add the complaint. :P
>
> (Sorry for being so critical but at least I tried to make it
> constructive...)

Just slice the const array to get a range. The specialization for ranges 
does not have the bug.

import std.algorithm;
void main() {
     const arr = [1, 2, 3];
     reduce!"a*b"(arr[]);   // It works.
}


More information about the Digitalmars-d mailing list