Why is std.algorithm.reduce impure?

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Mar 6 15:00:16 PST 2012


On Tue, Mar 06, 2012 at 11:51:05PM +0100, Adam D. Ruppe wrote:
> On Tuesday, 6 March 2012 at 22:48:30 UTC, H. S. Teoh wrote:
> >Oh? what's wrong with the const?
> 
> test10.d(3): Error: function test10.product without 'this' cannot be
> const/immutable
> 
> It works if you put parens on it:
> 
>         pure const(int) product(int[] args) {
> 
> 
> Without the parenthesis, D wants to apply it to this,
> like if you write void foo() const {} in C++.

But why can't 'this' be const? For example, why does the compiler reject
this:

	class A {
		int[] data;
		pure const int sum() {
			return reduce!"a*b"(data);
		}
	}

I'm not modifying data at at all, so why should it be an error?


T

-- 
Don't modify spaghetti code unless you can eat the consequences.


More information about the Digitalmars-d-learn mailing list