status of 'lazy' ?

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Sat Feb 17 09:11:17 PST 2007


Michiel wrote:
> Andrei Alexandrescu (See Website For Email) wrote:
> 
>> Just tell me
>> quickly what "const inout lazy const char[]" means...
> 
> I'm not convinced that type expression makes sense. I understand why you
> can probably make some parts of a type expression const and leave others
> non-const (like: a const pointer to a non-const value, or the other way
> around). But not how those two consts in a row there could make a
> difference. Nor how const and inout would work together. (const would
> probably neutralize the out.) Nor how const/lazy or inout/lazy would
> work together. But I don't know enough about 'lazy' to get deep into that.

That's (part of) the problem. Lazy and inout are easiest defined as type 
constructors (e.g. "lazy T" is a distinct type from "T"). For example, 
it makes sense to repeat it: "lazy lazy T" is a delegate returning a 
delegate that returns a T.

Making lazy and inout into qualifiers is very hard.

>> I don't have time to get busy with it just now, but here's a warmup
>> question: what should, and what shouldn't pass the semantic check below?
>>
>> void Fun(T, U)(T t, U u)
>> {
>>   t = 0;
>>   ++t;
>>   u[0] = 'a';
>>   u = "blah";
>> }
> 
> I think it would be like this:
> 
>> void main()
>> {
>>   const int x = 0; // No problem
>>   const char[] y = "wyda"; // No problem
>>   Fun(x, y); // No problem
>>   x = 1; // ERROR
>>   y[0] = 'a'; // NOT CERTAIN
>>   y = "geeba"; // ERROR
>> }
> 
> Correct me if I'm wrong.

The "NOT CERTAIN" case is an error because you can't modify the memory 
referred by a const object. There are errors in Fun too:

void Fun(T, U)(T t, U u)
{
   t = 0; // yah
   ++t;   // yah
   u[0] = 'a'; // nah
   u = "blah";  // yah
}

This all leads to a definition of const that is sound, but hard to explain:

1. "const T" refers only to memory indirectly addressed by T.
2. "const T name = expression" extends constness to names as well. 
(Another way to put it: const also acts as a storage class for name.) 
(Yet another way to put it: data definitions with using const are also 
final.)

> Now, I'm not sure if it's (const char)[] or const (char[]). In other
> words: A const array of chars or an array of const chars. And I'm not
> sure if the second version would make sense anyway. Would a const
> (dynamic array) become a static array or something?

No, it's just that the invariant is that the array itself won't change, 
but it can be referred to by names and slices that are rebindable.


Andrei



More information about the Digitalmars-d mailing list