a more consistent const syntax

Rioshin an'Harthen rharth75 at hotmail.com
Mon Aug 6 05:12:44 PDT 2007


"Reiner Pope" <some at address.com> kirjoitti viestissä 
news:f95nqs$lk5$1 at digitalmars.com...
> Paul Collier wrote:
>> Rioshin an'Harthen wrote:
>>> Same list, this time a const func returning a const return value:
>>>
>>> const const(P) func (P p)
>>> const(P) const func (P p)
>>> const(P) const(func) (P p)
>>> const(P) func const (P p)
>>> const(P) func:const (P p)
>>> const(P) func (P p) const
>>>
>>> I definitely prefer the last one as the cleanest of these.
>>
>> Just chiming in on a slightly different note... the line that stuck out 
>> in both examples for me was actually the const(func) line. That seems 
>> really intuitive and consistent with the const syntax elsewhere.
>>
>> I do find the const-on-the-end readable too, but really mostly because of 
>> familiarity with C++ ;)
>
> Although the idea is nice, I tried it out, and I don't like the look of 
> it:
>
> int const(getFoo)() { return foo; }
>
> My main objection is that the const() is around getFoo, so you're saying 
> that the function won't change -- but how can it, it's static data. What 
> you really mean when you are saying it's a const function is that the this 
> object is constant. So how about that?
>
> const(this) int getFoo() { return foo; }
>
> (It's syntactically unambiguous because this is a keyword)

Ok, same list as above, once again, this time using const(this) for method 
constness (and dropping the const(func) from the list):

const(this) const(P) func (P p)
const(P) const(this) func (P p)
const(P) func const(this) (P p)
const(P) func:const(this) (P p)
const(P) func (P p) const(this)

Personally, I find all of these quite unreadable this time around, although 
I guess I could get used to it. Too many parenthesized consts.

I'll do even a list with the parameter of the function as a const:

const(this) const(P) func (const(P) p)
const(P) const(this) func (const(P) p)
const(P) func const(this) (const(P) p)
const(P) func:const(this) (const(P) p)
const(P) func (const(P) p) const(this)

This is getting to where my eyes start to bleed. ;)

I'd still have to say that the cleanest syntax, at least to me, seems to be 
the last one, with the method constness indicator after the parameter list. 




More information about the Digitalmars-d mailing list