const by default.
Dave
Dave_member at pathlink.com
Fri Jul 7 22:18:53 PDT 2006
Brad Roberts wrote:
> Walter Bright wrote:
>> class C { int m; }
>>
>> void bar(C c) { c.m = 3; } // ok
>>
>> void foo(in C c) { bar(c); } // ok
>
> I have a problem with this chain.. Foo shouldn't be allowed to pass a
> const thing into a function that wants a mutable thing.
I have a problem with that and this also:
void foo(in C c)
{ C d = c;
d.m = 3; // ok
}
If c is passed 'read-only' (meaning the intention of the parameter
passed 'in' was to use the members for informational purposes inside the
function only), why allow c to be assigned to a 'non-const'?
If you look at 'in' as meaning "read-only / informational" then neither
of these should be allowed. And if neither of these are allowed, then
the programmer and compiler could both have something to count on, right?
But, I guess there's still the problem of:
C _c;
void main()
{
C c = new C();
c.m = 100;
_c = c;
// lots of code
foo(c);
printf("%d\n",c.m); // now 1000 after returning from foo()
}
void foo(in C c)
{
_c.m = c.m * 10;
}
class C
{
int m;
}
Hmmm, would it then be feasible to do a run-time check for non-release
builds like this:
void foo(in C c)
{
/* inserted by the compiler */
assert(_c !is c,"non-local _c is a reference to an 'in' parameter");
/* inserted by the compiler */
_c.m = c.m * 10;
}
The same check could be used for any 'in' reference parameter (arrays,
pointers, AA's).
That way the programmer and compiler could both count on 'in' with the
same level of confidence they do with array bounds checking now.
- Dave
More information about the Digitalmars-d
mailing list