www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why is std.algorithm.reduce impure?

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
Why is std.algorithm.reduce not marked pure? It makes it impossible to
do things like this:

	pure const int product(int[] args) {
		return reduce!"a * b"(args);
	}


T

-- 
Life is unfair. Ask too much from it, and it may decide you don't deserve what
you have now either.
Mar 06 2012
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Tuesday, 6 March 2012 at 22:39:20 UTC, H. S. Teoh wrote:
 Why is std.algorithm.reduce not marked pure?

It doesn't have to be - templates are inferred to be pure or not. If you take the const off that signature, your example works in today's dmd.
Mar 06 2012
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Mar 06, 2012 at 11:42:00PM +0100, Adam D. Ruppe wrote:
 On Tuesday, 6 March 2012 at 22:39:20 UTC, H. S. Teoh wrote:
Why is std.algorithm.reduce not marked pure?

It doesn't have to be - templates are inferred to be pure or not. If you take the const off that signature, your example works in today's dmd.

Oh? what's wrong with the const? T -- Don't modify spaghetti code unless you can eat the consequences.
Mar 06 2012
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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++. The reason here is most the attributes are at the beginning, which is cool because this works: const { void foo() {} int bar() {} } etc. But if you put parens on it, it specifically applies to the int.
Mar 06 2012
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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.
Mar 06 2012
parent bearophile <bearophileHUGS lycos.com> writes:
H. S. Teoh:

 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?

I think it's a small bug in std.algorithm.reduce, is this in Bugzilla already? import std.algorithm: reduce; void main() { const data = [2, 3, 4]; int r1 = reduce!q{a * b}(0, data); // OK int r2 = reduce!q{a * b}(data); } In std.algorithm.reduce there is also this (now bug 2443 is fixed) at about line 723: // For now, just iterate using ref to avoid unnecessary copying. // When Bug 2443 is fixed, this may need to change. foreach(ref elem; r) { if(initialized) Bye, bearophile
Mar 06 2012
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Mar 06, 2012 at 03:00:16PM -0800, H. S. Teoh wrote:
[...]
 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?

[...] Actually, nevermind that. Looks like a compiler bug that got fixed in dmd, but hasn't been pulled into gdc yet. I'll just have to be patient. :-) T -- A mathematician is a device for turning coffee into theorems. -- P. Erdos
Mar 06 2012