digitalmars.D.learn - Why is std.algorithm.reduce impure?
- H. S. Teoh (8/8) Mar 06 2012 Why is std.algorithm.reduce not marked pure? It makes it impossible to
- Adam D. Ruppe (5/6) Mar 06 2012 It doesn't have to be - templates are inferred to be
- H. S. Teoh (5/13) Mar 06 2012 Oh? what's wrong with the const?
- Adam D. Ruppe (16/17) Mar 06 2012 test10.d(3): Error: function test10.product without 'this' cannot
- H. S. Teoh (13/26) Mar 06 2012 But why can't 'this' be const? For example, why does the compiler reject
- bearophile (16/27) Mar 06 2012 I think it's a small bug in std.algorithm.reduce, is this in Bugzilla al...
- H. S. Teoh (9/20) Mar 06 2012 [...]
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
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
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:Oh? what's wrong with the const? T -- Don't modify spaghetti code unless you can eat the consequences.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
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
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: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.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++.
Mar 06 2012
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
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