Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
March 03, 2011 Struct reference returning function and const members | ||||
---|---|---|---|---|
| ||||
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; |
March 03, 2011 Re: Struct reference returning function and const members | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tom | 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
|
March 03, 2011 Re: Struct reference returning function and const members | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | El 03/03/2011 03:47, Ali Çehreli escribió:
> 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
>
I know, but I don't want a const struct. It's like if const transitivity were going backwards, or something.
:(
|
March 03, 2011 Re: Struct reference returning function and const members | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tom | 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
|
March 03, 2011 Re: Struct reference returning function and const members | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tom | 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/ |
Copyright © 1999-2021 by the D Language Foundation