[Issue 7736] Regression(2.059 beta): opApply for immutable structs too

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Mar 29 21:27:52 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=7736


Kenji Hara <k.hara.pg at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


--- Comment #2 from Kenji Hara <k.hara.pg at gmail.com> 2012-03-29 21:28:19 PDT ---
This issue is introduced by fixing bug 7038.

(In reply to comment #0)
> This code used to work not too much time ago (maybe 2.056?):
> 
> 
> immutable struct Foo {
>     char stop;
>     int opApply(int delegate(ref int) dg) {
>         int result;
>         for (int i = 0; i < stop; i++) {
>             result = dg(i);
>             if (result) break;
>         }
>         return result;
>     }
> }
> void main() {
>     foreach (i; Foo(10)) {}
> }

In 2.058 and before, This code was equivalent to the following:

struct __Foo {
immutable:  // a type qualifier for whole the struct affects to all its members
  [snip] // same as original code
}
alias immutable(__Foo) Foo;  // Foo is always immutable(__Foo)
void main() {
    foreach (i; Foo(10)) {}  
    // Foo(10) is an immutable object, so immutable opApply works as well.
}

> DMD 2.059 gives:
> 
> test.d(13): Error: cannot uniquely infer foreach argument types

After fixing bug 7038, 'Foo' is always mutable type.

struct Foo {
immutable:  // same as 2.058 and earlier
  [snip] // same as original code
}
void main() {
    foreach (i; Foo(10)) {}  
    // Foo(10) is a mutable object, so immutable opApply *doesn't work* as
well.
}

> The code compiles and works if you remove the "immutable" and replace it with
> const:
> 
> const struct Foo {
>     char stop;
>     int opApply(int delegate(ref int) dg) {
>         int result;
>         for (int i = 0; i < stop; i++) {
>             result = dg(i);
>             if (result) break;
>         }
>         return result;
>     }
> }
> void main() {
>     foreach (i; Foo(10)) {}
> }

Because const opApply works with mutable object.

Therefore, this is a "resolved-invalid bug" in 2.059, not a regression.

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


More information about the Digitalmars-d-bugs mailing list