December 03, 2007 Re: Why the need for an only const ref? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matti Niemenmaa | Matti Niemenmaa wrote:
> Janice Caron wrote:
>>> Walter, the BD of D, has been known to change his mind.
>> Sorry, I don't understand what BD of D means.
>
> BD is presumably short for "benevolent dictator".
>
I thought it meant "Brain Damage".
|
December 03, 2007 Re: Why the need for an only const ref? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Dec 3, 2007 10:35 AM, Walter Bright <newshound1@digitalmars.com> wrote:
> Janice Caron wrote:
> > On 12/1/07, Derek Parnell <derek@psych.ward> wrote:
> >> What is a D code example that catches inadverant reallocations at compile time?
> >
> > Just don't do it. Head const is a purely local thing. The designer of a function knows what it's supposed to do, and if they make it do something stupid, it's a bug
>
> That's right. Const-correctness is a thing that becomes necessary when crossing API boundaries, it is of only minor utility for local references only.
Ooh, ooh - I just figured out - we DO have head constness in D after all! You want a mutable buffer you can't move? No problem! Here's how it's done...
private char[] buffer_ = new char[MAXBUFSIZE];
char[] buffer() { return buffer_; }
buffer[0] = '+'; /* OK */
buffer ~= '+'; /* Error */
|
December 03, 2007 Re: Why the need for an only const ref? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | Janice Caron wrote:
> Ooh, ooh - I just figured out - we DO have head constness in D after
> all! You want a mutable buffer you can't move? No problem! Here's how
> it's done...
>
> private char[] buffer_ = new char[MAXBUFSIZE];
> char[] buffer() { return buffer_; }
>
> buffer[0] = '+'; /* OK */
> buffer ~= '+'; /* Error */
Right...now just make sure your modules are small. People using this tactic don't have many friends.
|
December 03, 2007 Re: Why the need for an only const ref? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Christopher Wright | On Dec 3, 2007 2:14 PM, Christopher Wright <dhasenan@gmail.com> wrote:
> Right...now just make sure your modules are small. People using this tactic don't have many friends.
What's the problem? Read references to buffer get turned into calls to buffer(), and those get /inlined/ by the compiler back to straightfoward reads of buffer_ (which is private). Write references are syntax errors.
Seems to me it works a treat. Why does this not make you happy?
|
December 03, 2007 Re: Why the need for an only const ref? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | Janice Caron wrote:
> On Dec 3, 2007 2:14 PM, Christopher Wright <dhasenan@gmail.com> wrote:
>> Right...now just make sure your modules are small. People using this
>> tactic don't have many friends.
>
> What's the problem? Read references to buffer get turned into calls to
> buffer(), and those get /inlined/ by the compiler back to
> straightfoward reads of buffer_ (which is private). Write references
> are syntax errors.
>
> Seems to me it works a treat. Why does this not make you happy?
I think his point is that private means nothing *within* a module, only between modules. So if you write your program as one big module with that trick then it's basically like you didn't use it at all.
--bb
|
December 03, 2007 Re: Why the need for an only const ref? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | On Dec 3, 2007 3:05 PM, Bill Baxter <dnewsgroup@billbaxter.com> wrote:
> I think his point is that private means nothing *within* a module, only between modules. So if you write your program as one big module with that trick then it's basically like you didn't use it at all.
Hmm. But it will work even /without/ the word private. That's really just there for documentation.
As I understood it, the goal was to catch reallocation bugs at compile time, so, say you have a function that looks like this:
void f(char[] buffer)
{
/*...*/
}
you can just change it to
void f(char[] buffer_)
{
char[] buffer() { return buffer_; }
/*...*/
}
and immediately you get compile-time checking. It's not whether or not you make the original variable private or not that matters, it's just the fact that you must always refer to it by its function-call-name. That's why I added the underscore to the "real" variable, but if that's not a strong enough deterent then make it
void f(char[] WARNING_doNotUseThisBuffer)
{
char[] buffer() { return WARNING_doNotUseThisBuffer; }
/*...*/
}
(but personally I think the trailing underscore is enough). There's no need for any of this to "spread" to the outside world, because head-constness is only local anyway.
|
December 03, 2007 Re: Why the need for an only const ref? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | Janice Caron wrote:
> On Dec 3, 2007 3:05 PM, Bill Baxter <dnewsgroup@billbaxter.com> wrote:
>> I think his point is that private means nothing *within* a module, only
>> between modules. So if you write your program as one big module with
>> that trick then it's basically like you didn't use it at all.
>
> Hmm. But it will work even /without/ the word private. That's really
> just there for documentation.
Ah, ok. I missed that. So it's the "not-an-lvalue" error you get then?
--bb
|
December 03, 2007 Re: Why the need for an only const ref? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | On 12/3/07, Bill Baxter <dnewsgroup@billbaxter.com> wrote:
> Ah, ok. I missed that. So it's the "not-an-lvalue" error you get then?
I think the error I got was "cannot append to char[]()". So basically, yes.
|
Copyright © 1999-2021 by the D Language Foundation