[Issue 1698] New: foreach auto type inference doesnt work properly
Sönke Ludwig
ludwig at informatik_dot_uni-luebeck.de
Sat Dec 1 09:22:31 PST 2007
d-bugmail at 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 at digitalmars.com
> ReportedBy: spam at 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.
More information about the Digitalmars-d-bugs
mailing list