www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1698] New: foreach auto type inference doesnt work properly

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

           Summary: foreach auto type inference doesnt work properly
           Product: D
           Version: 2.008
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: spam extrawurst.org


since the const/invariant changes in dmd2.008 the following valid code wont
compile anymore, cause the compile is not able to infer the type for c
correctly i think.

[CODE]
void bar(ref char c) {

}

void main() {

        string text;

        foreach(ref c; text) { // adding type char makes it work

                bar(c);
        }
}
[\CODE]

compiler message:
Error: cast(char)c is not an lvalue


-- 
Nov 29 2007
next sibling parent reply =?ISO-8859-15?Q?S=F6nke_Ludwig?= writes:
d-bugmail puremagic.com wrote:
 http://d.puremagic.com/issues/show_bug.cgi?id=1698
 
            Summary: foreach auto type inference doesnt work properly
            Product: D
            Version: 2.008
           Platform: PC
         OS/Version: Windows
             Status: NEW
           Keywords: rejects-valid
           Severity: normal
           Priority: P2
          Component: DMD
         AssignedTo: bugzilla digitalmars.com
         ReportedBy: spam extrawurst.org
 
 
 since the const/invariant changes in dmd2.008 the following valid code wont
 compile anymore, cause the compile is not able to infer the type for c
 correctly i think.
 
 [CODE]
 void bar(ref char c) {
 
 }
 
 void main() {
 
         string text;
 
         foreach(ref c; text) { // adding type char makes it work
 
                 bar(c);
         }
 }
 [\CODE]
 
 compiler message:
 Error: cast(char)c is not an lvalue
 
 
Since c inside of the foreach-loop is actually a "ref invariant(char)" (string = invariant(char)[]), it is correct that it cannot be passed to bar, taking a mutable "ref char". Changing bar to "void bar( ref invariant(char) c ){}" makes it compile again.
Dec 01 2007
parent reply "Janice Caron" <caron800 googlemail.com> writes:
On 12/1/07, Sönke Ludwig <ludwig informatik_dot_uni-luebeck.de> wrote:
 Since c inside of the foreach-loop is actually a "ref invariant(char)"
 (string = invariant(char)[]), it is correct that it cannot be passed to
 bar, taking a mutable "ref char".
 Changing bar to "void bar( ref invariant(char) c ){}" makes it compile
 again.
So... can you /assign/ a ref invariant(char)? I'm confused as to why the code isn't just foreach(c;text)
Dec 01 2007
parent =?ISO-8859-1?Q?S=F6nke_Ludwig?= writes:
Janice Caron schrieb:
 On 12/1/07, Sönke Ludwig <ludwig informatik_dot_uni-luebeck.de> wrote:
 Since c inside of the foreach-loop is actually a "ref invariant(char)"
 (string = invariant(char)[]), it is correct that it cannot be passed to
 bar, taking a mutable "ref char".
 Changing bar to "void bar( ref invariant(char) c ){}" makes it compile
 again.
So... can you /assign/ a ref invariant(char)? I'm confused as to why the code isn't just foreach(c;text)
You surprisingly can.. actually you can do: import std.stdio; void main() { string text = "Hello, World!".idup; foreach( ref c; text ) c = 'X'; writefln(text); // prints "XXXX... } But you cannot do "text[0] = 'X';". So it seems to me arrays with the new const system are actually quite broken for now.
Dec 03 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1698


spam extrawurst.org changed:

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





you are right, my bad.


-- 
Dec 01 2007