Const template
Andrei Alexandrescu (See Website For Email)
SeeWebsiteForEmail at erdani.org
Mon Jan 22 08:38:39 PST 2007
Bruno Medeiros wrote:
> Andrei Alexandrescu (See Website For Email) wrote:
>> Today, Walter and myself have come with a semantics for const (as in,
>> "read-only view" that) seems to be a good starting point. The details
>> are yet to be ironed out, but here's the basic plot:
>>
>
> So the keyword for the "read-only view" will be 'const' too? It will
> share space with the 'const' as in "compile time constant" ?
Yes. This is arguably a weak point of the design.
>> * Shallow const is achievable via final. Final only means a value is
>> not supposed to be changed, but anything reachable through it can be
>> mutated.
>>
>
> So are we going to use the 'final' keyword for that? That's good since
> it clears up the overloaded meaning of 'const' in current D. (used both
> for compile time constants and final)
That is correct. An easy way to remember both is: final refers to a name
(the name cannot be rebound), const refers to memory accessible through
a name (the memory cannot be changed).
>> * D avoids the issue of duplicated function bodies in the following way:
>>
>> struct Widget
>> {
>> storageof(this) int* Foo(storageof(this))(int i) { ... }
>> }
>>
>
> Hum, "storageof"? Shouldn't the name be different, since these aren't
> really storage classes?
I think const can be considered a storage class even when it is
"borrowed" from data that originally was not const.
>> * Constructors and destructors can figure the storage class of the
>> object being constructed. This is useful for selecting different
>> allocation strategies for immutable vs. mutable objects:
>>
>> class Widget
>> {
>> this(int i) { ... }
>> this(const)(int i) { ... }
>> ~this() { ... }
>> ~this(const)() { ... }
>> }
>>
>> Const cdtors can obviously mutate the object being cdted. In a cdtor,
>> 'const' is not enforced -- it's just an information for the programmer.
>>
>
> Whoa, huh? What's the meaning of a const constructor or of a const
> destructor? How would they even be invoked?
class Widget { ... }
auto a = new Widget; // invokes this()
auto b = new const Widget; // invokes this(const)()
Andrei
More information about the Digitalmars-d
mailing list