what are const scope parameters?
Denton Cockburn
diboss at hotmail.com
Sat Mar 8 04:15:35 PST 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:
>>
>> 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);
}
More information about the Digitalmars-d-learn
mailing list