digitalmars.D.learn - what are const scope parameters?
- Denton Cockburn (3/3) Mar 07 2008 and what are they good for?
- Jason House (3/7) Mar 07 2008 I believe the difference is that in your first case x can't be kept past...
- Denton Cockburn (35/41) Mar 07 2008 That's what I used to think, but look at this in D2:
- Jason House (5/56) Mar 07 2008 cast at your own risk ;)
- Denton Cockburn (6/11) Mar 07 2008 Ok, without casts. The point is that the scope specification doesn't
- Denton Cockburn (38/51) Mar 08 2008 doh! Forgot to include the cast-free code:
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
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
On Fri, 07 Mar 2008 21:09:45 -0500, Jason House wrote:Denton Cockburn wrote: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?[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.
Mar 07 2008
Denton Cockburn wrote:On Fri, 07 Mar 2008 21:09:45 -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 ;)Denton Cockburn wrote: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?[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.
Mar 07 2008
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
On Sat, 08 Mar 2008 00:24:23 -0500, Denton Cockburn wrote:On Fri, 07 Mar 2008 23:19:02 -0500, Jason House wrote: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); }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 08 2008