Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
November 29, 2007 [Issue 1698] New: foreach auto type inference doesnt work properly | ||||
---|---|---|---|---|
| ||||
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 -- |
December 01, 2007 Re: [Issue 1698] New: foreach auto type inference doesnt work properly | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | 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.
|
December 01, 2007 [Issue 1698] foreach auto type inference doesnt work properly | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | 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. -- |
December 01, 2007 Re: [Issue 1698] New: foreach auto type inference doesnt work properly | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sönke Ludwig | 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) |
December 03, 2007 Re: [Issue 1698] New: foreach auto type inference doesnt work properly | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | 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.
|
Copyright © 1999-2021 by the D Language Foundation