digitalmars.D.bugs - [Issue 1698] New: foreach auto type inference doesnt work properly
- d-bugmail puremagic.com (29/29) Nov 29 2007 http://d.puremagic.com/issues/show_bug.cgi?id=1698
- =?ISO-8859-15?Q?S=F6nke_Ludwig?= (6/46) Dec 01 2007 Since c inside of the foreach-loop is actually a "ref invariant(char)"
- Janice Caron (4/9) Dec 01 2007 So... can you /assign/ a ref invariant(char)?
- =?ISO-8859-1?Q?S=F6nke_Ludwig?= (13/26) Dec 03 2007 You surprisingly can.. actually you can do:
- d-bugmail puremagic.com (9/9) Dec 01 2007 http://d.puremagic.com/issues/show_bug.cgi?id=1698
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
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 lvalueSince 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
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
Janice Caron schrieb:On 12/1/07, Sönke Ludwig <ludwig informatik_dot_uni-luebeck.de> wrote: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.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 03 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1698 spam extrawurst.org changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution| |INVALID ------- Comment #2 from spam extrawurst.org 2007-12-01 11:55 ------- you are right, my bad. --
Dec 01 2007