a more consistent const syntax
Paul Collier
paching at gmail.com
Sun Aug 5 20:13:55 PDT 2007
Reiner Pope wrote:
> Paul Collier wrote:
>> Rioshin an'Harthen wrote:
>>> "Chris Nicholson-Sauls" <ibisbasenji at gmail.com> kirjoitti viestissä
>>> news:f951kq$2fss$1 at digitalmars.com...
>>>> Daniel919 wrote:
>>>>> 3. const P func(P p) { ... }
>>>>> reads like: func returns a const(P)
>>>>
>>>> Which is, indeed, a problem -- in that I agree.
>>>
>>> I agree, as well. It reads like returning a const P.
>>>
>>>>> const / invariant alone (without brackets) is an attribute and has
>>>>> no other meaning
>>>>> further proposal: returned types in a bracket at the end:
>>>>> 3. const func (P p) (P) { ... }
>>>>> //templated syntax: const func!(T) (T p) (T) { ... }
>>>>
>>>> Uhm. Ew. No, seriously, I just could not possibly handle that; I
>>>> would keep thinking I saw templates where there aren't any. Worse
>>>> yet, for a long time I'll see the '!(' and keep wondering how I
>>>> could be instantiating a template in that position, when its really
>>>> a template declaration. T'is a naughty naughty thing to mix the two
>>>> -- would give both the compiler and the user headaches. IMHO, its
>>>> the 'const'/'invariant' keyword on methods that needs to move -- not
>>>> sure where it should go, though. What looks best down here? ;)
>>>> (First listing is the current state, for reference.)
>>>>
>>>> const P vunc (P p) { ... }
>>>> P const func (P p) { ... }
>>>> P const:func (P p) { ... }
>>>> P const(func) (P p) { ... }
>>>> P func const (P p) { ... }
>>>> P func:const (P p) { ... }
>>>> P func (P p) const { ... }
>>>
>>> Definitely the last one. It's immediately familiar to anyone with a
>>> C++ background, which I guess most of those coming to D has, and
>>> which I think are the people Walter is especially targetting. It also
>>> has the added bonus of not complicating method declaration grammar
>>> too much.
>>>
>>> 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)
>
> -- Reiner
Yeah, I guess there are a lot of tradeoffs with each syntax. I was
wondering what the function pointers would look like as well...
const int function() p; // Current
const(this) int function() p; // Could be confusing in a member function
int const(function)() p; // Looks like a const function pointer
int function() const p; // C++ style
And combinations...
const(const const(int) function(const(int))) p;
const(const(this) const(int) function(const(int))) p;
const(const(int) const(function)(const(int))) p;
const(const(int) function(const(int)) const) p;
But the latter isn't really valid material for comparison. Anyways, I
think the bikeshed should be orange! ;)
More information about the Digitalmars-d
mailing list