www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Wrong const attribute?

reply Paolo Invernizzi <arathorn fastwebnet.it> writes:
--Apple-Mail-7--919646864
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

Hi all,=20

I've found nothing on bugzilla for that, what I'm missing? Or it's a =
bug? (DMD 2.055)

struct Bar {
    immutable int i;
    this(int j){ i =3D j; }
}

struct Foo {
    Bar bar;
}

void main(){
   =20
    auto b =3D Bar(1);
   =20
    auto f =3D Foo();
    f.bar =3D Bar(2); // Error: can only initialize const member bar =
inside constructor
   =20
}

Cheers,=20
Paolo Invernizzi



--Apple-Mail-7--919646864
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=us-ascii

<html><head></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi =
all,&nbsp;<div><br></div><div>I've found nothing on bugzilla for that, =
what I'm missing? Or it's a bug? (DMD =
2.055)</div><div><br></div><div><div style=3D"margin-top: 0px; =
margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal =
normal normal 11px/normal Monaco; "><span style=3D"text-decoration: =
underline ; color: #900868">struct</span> Bar {</div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; font: normal normal normal 11px/normal Monaco; color: =
rgb(144, 8, 104); "><span style=3D"color: #000000">&nbsp; &nbsp; =
</span>immutable<span style=3D"color: #000000"> </span><span =
style=3D"text-decoration: underline">int</span><span style=3D"color: =
#000000"> </span><span style=3D"color: #1f13cb">i</span><span =
style=3D"color: #000000">;</span></div><div style=3D"margin-top: 0px; =
margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal =
normal normal 11px/normal Monaco; ">&nbsp; &nbsp; <span style=3D"color: =
#900868">this</span>(<span style=3D"text-decoration: underline ; color: =
#900868">int</span> j){ <span style=3D"color: #1f13cb">i</span> =3D j; =
}</div><div style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: =
0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; =
">}</div><div style=3D"margin-top: 0px; margin-right: 0px; =
margin-bottom: 0px; margin-left: 0px; font: normal normal normal =
11px/normal Monaco; min-height: 15px; "><br></div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><span =
style=3D"text-decoration: underline ; color: #900868">struct</span> =
<span style=3D"text-decoration: underline">Foo</span> {</div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; font: normal normal normal 11px/normal Monaco; =
">&nbsp; &nbsp; Bar <span style=3D"color: #1f13cb">bar</span>;</div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; font: normal normal normal 11px/normal Monaco; =
">}</div><div style=3D"margin-top: 0px; margin-right: 0px; =
margin-bottom: 0px; margin-left: 0px; font: normal normal normal =
11px/normal Monaco; min-height: 15px; "><br></div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><span =
style=3D"color: #900868">void</span> main(){</div><p style=3D"margin: =
0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: =
15.0px">&nbsp;&nbsp; &nbsp;<br class=3D"webkit-block-placeholder"></p><div=
 style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; font: normal normal normal 11px/normal Monaco; =
">&nbsp; &nbsp; <span style=3D"color: #900868">auto</span> b =3D =
Bar(1);</div><p style=3D"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px =
Monaco; min-height: 15.0px">&nbsp;&nbsp; &nbsp;<br =
class=3D"webkit-block-placeholder"></p><div style=3D"margin-top: 0px; =
margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal =
normal normal 11px/normal Monaco; ">&nbsp; &nbsp; <span style=3D"color: =
#900868">auto</span> f =3D <span style=3D"text-decoration: =
underline">Foo</span>();</div><div style=3D"margin-top: 0px; =
margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal =
normal normal 11px/normal Monaco; color: rgb(83, 145, 114); "><span =
style=3D"color: #000000">&nbsp; &nbsp; f.</span><span style=3D"color: =
#1f13cb">bar</span><span style=3D"color: #000000"> =3D Bar(2); </span>// =
Error: can only initialize <span style=3D"text-decoration: =
underline">const</span> member bar inside constructor</div><p =
style=3D"margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; =
min-height: 15.0px">&nbsp;&nbsp; &nbsp;<br =
class=3D"webkit-block-placeholder"></p><div style=3D"margin-top: 0px; =
margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal =
normal normal 11px/normal Monaco; ">}</div></div><div style=3D"margin-top:=
 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: =
normal normal normal 11px/normal Monaco; "><br></div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; font: normal normal normal 11px/normal Monaco; =
">Cheers,&nbsp;</div><div style=3D"margin-top: 0px; margin-right: 0px; =
margin-bottom: 0px; margin-left: 0px; font: normal normal normal =
11px/normal Monaco; ">Paolo Invernizzi</div><div style=3D"margin-top: =
0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: =
normal normal normal 11px/normal Monaco; "><br></div><div =
style=3D"margin-top: 0px; margin-right: 0px; margin-bottom: 0px; =
margin-left: 0px; font: normal normal normal 11px/normal Monaco; =
"><br></div></body></html>=

--Apple-Mail-7--919646864--
Sep 22 2011
parent travert phare.normalesup.org (Christophe) writes:
Paolo Invernizzi , dans le message (digitalmars.D.learn:29680), a
 écrit :
 
 --Apple-Mail-7--919646864
 Content-Transfer-Encoding: quoted-printable
 Content-Type: text/plain;
 	charset=us-ascii
 
 Hi all,=20
 
 I've found nothing on bugzilla for that, what I'm missing? Or it's a =
 bug? (DMD 2.055)
 
 struct Bar {
     immutable int i;
     this(int j){ i =3D j; }
 }
 
 struct Foo {
     Bar bar;
 }
 
 void main(){
    =20
     auto b =3D Bar(1);
    =20
     auto f =3D Foo();
     f.bar =3D Bar(2); // Error: can only initialize const member bar =
 inside constructor
    =20
 }
 

Since your Bar has am immutable member, you cannot assign a Bar instance, which is what you do in the line where there is an error. If you want to perform an assignment, Bar.i cannot be immutable. If you really want Bar.i to be immutable, Foo must hold a pointer to Bar, so you can change this pointer, or Bar can be a class instead of a struct (which means Foo does contain a pointer to Bar, but you don't have to notice it). -- Christophe Travert
Sep 23 2011