Why can't we define re-assignable const reference variable?
Steven Schveighoffer
schveiguy at yahoo.com
Wed Feb 20 12:22:30 PST 2008
"Sergey Gromov" wrote
> Steven Schveighoffer wrote:
>> "Sergey Gromov" wrote
>> > If you create a generic algorithm which works with T, and declare
>> > something as
>> >
>> > const(T) t;
>> >
>> > then you effectively assert that you're going to replace t with
>> > different other Ts, but promise to keep integrity of a referenced T
>> > intact, if anything is referenced at all. This means that for int,
>> > which is not referencing anything,
>> >
>> > const(int) t;
>> >
>> > is completely and safely equivalent to
>> >
>> > int t;
>> >
>> > without breaking any contracts of your algorithm. Please provide an
>> > example where "const(int) x;" absolutely must mean "const int x;" for a
>> > generic algorithm to work properly.
>>
>> Sergey, I am not arguing that the functionality that you wish to have is
>> invalid. In fact, I would have been ok with that syntax, and understood
>> it
>> once it was explained thoroughly. But I have to say that I agree with
>> Janice and Walter on this one, it was very confusing.
>>
>> It just goes against the traditional usage of parentheses. The idea is
>> correct, the syntax is confusing. Find another syntax.
>
> The traditional usage of parentheses is this: foo(x). This means that a
> function foo is applied to its argument x, and not applied to everything
> else. You are probably talking about some different tradition. Please
> clarify.
That is the usage I am referring to. I'll spell it out in English:
class C means that C is equivalent to a "reference to a some data" that
compromises the class that follows the given definition.
If I interpret const(C) as constant C, or substituting for the english
definition of C, a constant reference to some data, it is natural to assume
that both the data AND the reference are const. It does not make intuitive
sense that const(C) means reference to some constant data, where the const
applies to the data, but not the reference, even though the reference is in
C. It makes even less sense that const(int) means mutable int. X * means a
pointer to X, so const(X) * means a pointer to a constant X, that makes
sense. But since classes do not have the reference separated in syntax,
there is no way to bring that piece of it outside the parentheses. This is
why we need a new syntax, to make the definition clear and unambiguous.
Your intuition may vary from 99% of us here, but that doesn't really matter.
Your solution isn't intuitive, it is confusing, it would hinder the adoption
of D 2. Walter is trying to attract as many coders as possible, not just
you ;)
-Steve
More information about the Digitalmars-d
mailing list