July 07, 2006 Re: const by default. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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.
|
July 08, 2006 Re: const by default. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Roberts | 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
|
July 08, 2006 Re: const by default. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Roberts | 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.
Brad ~ you might be interested in the thread over in d.learn?
The premise is that, assuming readonly arrays can be supported, everything else can be made readonly via encapsulation and accessor methods. It would tend to make for a pretty simple implementation, along with simple rules?
The thread is called "read only array reference", but perhaps should be called "bang for the buck" instead
|
July 09, 2006 Re: const by default. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Fri, 07 Jul 2006 10:42:41 -0700, Walter Bright <newshound@digitalmars.com> wrote:
> void bar(C c) { c.m = 3; } // ok
> void foo(in C c) { c.m = 3; } // error
I don't like that you're differentiating between implicit and explicit use of 'in'.
I assume you're doing this to preserve backward compatibility with existing D code and/or to avoid having to create/add a mutable keyword?
Regan
|
Copyright © 1999-2021 by the D Language Foundation