www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - what are const scope parameters?

reply Denton Cockburn <diboss hotmail.com> writes:
and what are they good for?

I'm trying to understand the difference between:
"in Foo x" and "const(Foo) x" in D2.
Mar 07 2008
parent reply Jason House <jason.james.house gmail.com> writes:
Denton Cockburn wrote:

 and what are they good for?
 
 I'm trying to understand the difference between:
 "in Foo x" and "const(Foo) x" in D2.
I believe the difference is that in your first case x can't be kept past the function lifetime and in the second case, a copy of x can be kept.
Mar 07 2008
parent reply Denton Cockburn <diboss hotmail.com> writes:
On Fri, 07 Mar 2008 21:09:45 -0500, Jason House wrote:

 Denton Cockburn wrote:
 
 [quoted text muted]
I believe the difference is that in your first case x can't be kept past the function lifetime and in the second case, a copy of x can be kept.
That's what I used to think, but look at this in D2: import std.stdio; class C { int x; } // with in and const, neither function can change the parameter C foo(in C c) { C d = new C; d.x = c.x; writefln("foo.d.x = ", d.x); return cast(C)c; // yes, we've casted away the const } C bar(const C c) { C d = new C; d.x = c.x; // we've made use of c writefln("bar.d.x = ", d.x); return cast(C)c; } void main() { C c = new C; c.x = 5; C c2 = foo(c); c = bar(c2); // CLEARLY c2 is still alive here, so what has scope done? writefln(c.x); } both styles work (the same apparently). So what did the scope part of the in (which is equal to 'const scope') accomplish?
Mar 07 2008
parent reply Jason House <jason.james.house gmail.com> writes:
Denton Cockburn wrote:

 On Fri, 07 Mar 2008 21:09:45 -0500, Jason House wrote:
 
 Denton Cockburn wrote:
 
 [quoted text muted]
I believe the difference is that in your first case x can't be kept past the function lifetime and in the second case, a copy of x can be kept.
That's what I used to think, but look at this in D2: import std.stdio; class C { int x; } // with in and const, neither function can change the parameter C foo(in C c) { C d = new C; d.x = c.x; writefln("foo.d.x = ", d.x); return cast(C)c; // yes, we've casted away the const } C bar(const C c) { C d = new C; d.x = c.x; // we've made use of c writefln("bar.d.x = ", d.x); return cast(C)c; } void main() { C c = new C; c.x = 5; C c2 = foo(c); c = bar(c2); // CLEARLY c2 is still alive here, so what has scope done? writefln(c.x); } both styles work (the same apparently). So what did the scope part of the in (which is equal to 'const scope') accomplish?
cast at your own risk ;) Having c2 still alive in main after the call to bar is ok and expected. The idea is that bar can't keep it but gives no restrictions on the caller. The cast that breaks this stuff is your fault ;)
Mar 07 2008
parent reply Denton Cockburn <diboss hotmail.com> writes:
On Fri, 07 Mar 2008 23:19:02 -0500, Jason House wrote:
 
 cast at your own risk ;)
 Having c2 still alive in main after the call to bar is ok and expected.  The
 idea is that bar can't keep it but gives no restrictions on the caller. 
 The cast that breaks this stuff is your fault ;)
Ok, without casts. The point is that the scope specification doesn't affect the variable at all from what I can see. I'm thinking the two specifications are the same ("in X p" and "const(X) p") I need to see an example or an explanation of a situation in which it doesn't hold. I wasn't able to find anything in the docs.
Mar 07 2008
parent Denton Cockburn <diboss hotmail.com> writes:
On Sat, 08 Mar 2008 00:24:23 -0500, Denton Cockburn wrote:

 On Fri, 07 Mar 2008 23:19:02 -0500, Jason House wrote:
 
 cast at your own risk ;)
 Having c2 still alive in main after the call to bar is ok and expected.  The
 idea is that bar can't keep it but gives no restrictions on the caller. 
 The cast that breaks this stuff is your fault ;)
Ok, without casts. The point is that the scope specification doesn't affect the variable at all from what I can see. I'm thinking the two specifications are the same ("in X p" and "const(X) p") I need to see an example or an explanation of a situation in which it doesn't hold. I wasn't able to find anything in the docs.
doh! Forgot to include the cast-free code: import std.stdio; class C { int x; } // with in and const, neither function can change the parameter const(C) foo(in C c) { C d = new C; d.x = c.x; writefln("foo.d.x = ", d.x); return c; } const(C) bar(const C c) { C d = new C; d.x = c.x; // we've made use of c writefln("bar.d.x = ", d.x); return c; } void main() { C c = new C; c.x = 5; auto c2 = foo(c); auto c3 = bar(c2); // CLEARLY c2 is still alive here, so what has scope done? writefln(c3.x); }
Mar 08 2008