Extended Type Design.
Andrei Alexandrescu (See Website For Email)
SeeWebsiteForEmail at erdani.org
Tue Mar 20 17:24:49 PDT 2007
Derek Parnell wrote:
> On Tue, 20 Mar 2007 16:01:35 -0700, Walter Bright wrote:
>
>
>> A symbol is a name to which is 'bound' a value.
> ...
>
>> Here, we bind a new value to the symbol x:
>> x = 4;
>
> I used to use the verb 'to assign' for this concept. I guess that's still
> okay or must I modernize <G>
You may want to modernize. "Assign" doesn't quite catch the notion of
indirect reference, and from a couple of posts I understand that this is
a source of confusion.
A very useful way to see "int x = 4;" is that the symbol x is bound to
the Platonic number 4. The 4 itself cannot change. You can rebind x by,
say, writing ++x. That unbinds x from Plato 4 and binds it to Plato 5.
Once this is clear, the notions of values and references clarifies a lot.
>> static int x = 3;
>>
>> '3' is the value.
>> 'int' is the type.
>> 'x' is the symbol.
>> 'static' is the storage class.
>>
>>
>> A storage class originally meant where the symbol is stored, such as in
>> the data segment, on the stack, in a register, or in ROM. It's been
>> generalized a bit since then. The main way to tell a storage class apart
>> is that:
>> 1) a storage class applies to the symbol
>
> "to the symbol"? Don't you mean "to the data that the symbol represents"?
> In the case above, the symbol is 'x', and I don't think 'x' is being stored
> anywhere except in the compiler's internal tables, and I'm sure 'static'
> isn't referring to the compiler's internals.
The meaning was simple. When you say "poor fella Jim", "poor" applies to
Jim, not to "fella".
>> 'invariant' is a guarantee that any data of that type will never change.
>
> class Foo
> {
> int a;
> char[] s;
> this(char[] d)
> {
> s = d.dup;
> a = s.length;
> }
> }
> invariant Foo f = new Foo("nice");
>
> f.a = 1; // fails??? changing f's data
> f.s = "bad"; // fails??? changing f's data
> f.s.length = 1; // fails??? changing f's data
> f.s[0] = 'r'; // okay ??? not changing f's data
They all fail. The last fails because invariance is transitive. All that
could be done is to rebind f to another invariant object.
Andrei
More information about the Digitalmars-d
mailing list