www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Struct reference returning function and const members

reply Tom <tom nospam.com> writes:
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
next sibling parent reply =?ISO-8859-1?Q?Ali_=C7ehreli?= <acehreli yahoo.com> writes:
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
parent Tom <tom nospam.com> writes:
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. :(
Mar 02 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
prev sibling parent Michel Fortin <michel.fortin michelf.com> writes:
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