www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9947] New: reduce of an array of structs with an immutable field

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9947

           Summary: reduce of an array of structs with an immutable field
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2013-04-16 16:16:16 PDT ---
Maybe this should work:


import std.algorithm: reduce;
struct Foo {
    immutable int x;
}
void main() {
    auto data = [Foo(0)];
    reduce!((a, b) => a)(data);
}



DMD 2.063alpha gives:

...\dmd2\src\phobos\std\algorithm.d(702): Error: cannot modify struct result
Foo with immutable members
...\dmd2\src\phobos\std\algorithm.d(723): Error: template instance
test.main.reduce!(__lambda3).reduce!(Foo,Foo[]) error instantiating
test.d(7):        instantiated from here: reduce!(Foo[])
test.d(7): Error: template instance test.main.reduce!(__lambda3).reduce!(Foo[])
error instantiating

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 16 2013
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9947


Ryuichi OHORI <r.97all gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |r.97all gmail.com


--- Comment #1 from Ryuichi OHORI <r.97all gmail.com> 2013-05-16 03:19:35 PDT
---
Using DMD32 v2.061 on Windows 32bit, below doesn't work too.
I think this is the same issue:
-----
import std.stdio;
import std.algorithm : sort;

struct S
{
    immutable int x;
}

void main()
{
    auto arr = [S(0), S(2), S(1)];
    sort!((a, b) => a.x < b.x)(arr);
    arr.writeln();
}
-----

algorithm.d(1814):Error: cannot modify struct lhs S with immutable members
algorithm.d(1815): Error: cannot modify struct rhs S with immutable members
algorithm.d(7972): Error: template instance std.algorithm.swap!(S) error
instantiating
algorithm.d(7908):   instantiated from here: swapAt!(S[])
algorithm.d(7993):   instantiated from here: getPivot!(__lambda3, S[])
algorithm.d(7712):   instantiated from here: quickSortImpl!(__lambda3, S[])
main.d(12):        instantiated from here: sort!(__lambda3,
cast(SwapStrategy)0, S[])
algorithm.d(7908): Error: template instance std.algorithm.swapAt!(S[]) error
instantiating
algorithm.d(7993):   instantiated from here: getPivot!(__lambda3, S[])
algorithm.d(7712):   instantiated from here: quickSortImpl!(__lambda3, S[])
main.d(12):        instantiated from here: sort!(__lambda3,
cast(SwapStrategy)0, S[])
algorithm.d(7993): Error: template instance main.main.getPivot!(__lambda3, S[])
error instantiating
algorithm.d(7712):   instantiated from here: quickSortImpl!(__lambda3, S[])
main.d(12):        instantiated from here: sort!(__lambda3,
cast(SwapStrategy)0, S[])
algorithm.d(7946): Error: cannot modify struct r[j] S with immutable members
algorithm.d(7949): Error: cannot modify struct r[j] S with immutable members
algorithm.d(8028): Error: template instance
main.main.optimisticInsertionSort!(__lambda3, S[]) error instantiating
algorithm.d(7712):   instantiated from here: quickSortImpl!(__lambda3, S[])
main.d(12):        instantiated from here: sort!(__lambda3,
cast(SwapStrategy)0, S[])
algorithm.d(7712): Error: template instance main.main.quickSortImpl!(__lambda3,
S[]) error instantiating
main.d(12):        instantiated from here: sort!(__lambda3,
cast(SwapStrategy)0, S[])
main.d(12): Error: template instance main.main.sort!(__lambda3,
cast(SwapStrategy)0, S[]) error instantiating

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 16 2013