digitalmars.D.learn - Struct reference returning function and const members
- Tom (30/30) Mar 02 2011 I have...
- =?ISO-8859-1?Q?Ali_=C7ehreli?= (7/37) Mar 02 2011 I don't know the full answer but returning a reference to const object
- Tom (4/51) Mar 02 2011 I know, but I don't want a const struct. It's like if const transitivity...
- Jonathan M Davis (8/45) Mar 02 2011 Well, it has something do to with the ref, since passing be value works....
- Michel Fortin (10/18) Mar 03 2011 I think the compiler complains because the s1.c member is not
I have... int main(string[] args) { auto s1 = f(); // MH MH auto s2 = g(); // OK s2.c = null; // OK return 0; } class C {} struct StructWithConstMember { this(int i, C c) { this.i=i; this.c=c; } int i; const(C) c; } struct StructWithoutConstMember { this(int i, C c) { this.i=i; this.c=c; } int i; C c; } ref StructWithConstMember f() { return * new StructWithConstMember(1, new C); // ERROR } ref StructWithoutConstMember g() { return * new StructWithoutConstMember(1, new C); // OK } And get the error... src\main.d(27): Error: *new StructWithConstMember(1,new C) is not mutable So I can't return a struct that has a const member? Why? Am I missing something something? Thanks, Tom;
Mar 02 2011
On 03/02/2011 10:42 PM, Tom wrote:I have... int main(string[] args) { auto s1 = f(); // MH MH auto s2 = g(); // OK s2.c = null; // OK return 0; } class C {} struct StructWithConstMember { this(int i, C c) { this.i=i; this.c=c; } int i; const(C) c; } struct StructWithoutConstMember { this(int i, C c) { this.i=i; this.c=c; } int i; C c; } ref StructWithConstMember f() { return * new StructWithConstMember(1, new C); // ERROR } ref StructWithoutConstMember g() { return * new StructWithoutConstMember(1, new C); // OK } And get the error... src\main.d(27): Error: *new StructWithConstMember(1,new C) is not mutable So I can't return a struct that has a const member? Why? Am I missing something something? Thanks, Tom;I don't know the full answer but returning a reference to const object works: ref const(StructWithConstMember) f() { return * new StructWithConstMember(1, new C); // now compiles } Ali
Mar 02 2011
El 03/03/2011 03:47, Ali Çehreli escribió:On 03/02/2011 10:42 PM, Tom wrote:I know, but I don't want a const struct. It's like if const transitivity were going backwards, or something. :(I have... int main(string[] args) { auto s1 = f(); // MH MH auto s2 = g(); // OK s2.c = null; // OK return 0; } class C {} struct StructWithConstMember { this(int i, C c) { this.i=i; this.c=c; } int i; const(C) c; } struct StructWithoutConstMember { this(int i, C c) { this.i=i; this.c=c; } int i; C c; } ref StructWithConstMember f() { return * new StructWithConstMember(1, new C); // ERROR } ref StructWithoutConstMember g() { return * new StructWithoutConstMember(1, new C); // OK } And get the error... src\main.d(27): Error: *new StructWithConstMember(1,new C) is not mutable So I can't return a struct that has a const member? Why? Am I missing something something? Thanks, Tom;I don't know the full answer but returning a reference to const object works: ref const(StructWithConstMember) f() { return * new StructWithConstMember(1, new C); // now compiles } Ali
Mar 02 2011
On Wednesday 02 March 2011 22:42:18 Tom wrote:I have... int main(string[] args) { auto s1 = f(); // MH MH auto s2 = g(); // OK s2.c = null; // OK return 0; } class C {} struct StructWithConstMember { this(int i, C c) { this.i=i; this.c=c; } int i; const(C) c; } struct StructWithoutConstMember { this(int i, C c) { this.i=i; this.c=c; } int i; C c; } ref StructWithConstMember f() { return * new StructWithConstMember(1, new C); // ERROR } ref StructWithoutConstMember g() { return * new StructWithoutConstMember(1, new C); // OK } And get the error... src\main.d(27): Error: *new StructWithConstMember(1,new C) is not mutable So I can't return a struct that has a const member? Why? Am I missing something something?Well, it has something do to with the ref, since passing be value works. I'm not sure if it's a bug or not. On the whole, I would _not_ advise that you have structs with const or immutable member variables. You can never reassign to them. And that may be related to why it's not working here. It may very well be a bug - I sure don't know why it isn't working here - but in general, having structs with const or immutable members probably isn't a good idea. - Jonathan M Davis
Mar 02 2011
On 2011-03-03 01:42:18 -0500, Tom <tom nospam.com> said:I have... int main(string[] args) { auto s1 = f(); // MH MH auto s2 = g(); // OK s2.c = null; // OK return 0; }I think the compiler complains because the s1.c member is not assignable (since it is const), so you can't assign to s1 as a whole. That said, it should probably be allowed for an initialization assignment like this one. I suggest you add it to the bug tracker. <http://d.puremagic.com/issues/> -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Mar 03 2011