Extended Type Design.

Derek Parnell derek at nomail.afraid.org
Tue Mar 20 16:53:17 PDT 2007


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>
 
> 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.

 
> '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

 f = new Foo("rabbit"); // okay 'cos 'f' is a reference
                        // and not the object???

> 'const' is a guarantee that any data of that type will never be modified 
> through a reference to that type (though other, non-const references to 
> that type can modify the data).

 const Foo f = new Foo("nice");
       Foo g = f;

 f.a = 1; // fails??? changing f's data
 g.a = 1; // okay??? Using 'g' and not 'f'.
 f.s = "bad"; // fails??? changing f's data
 g.s = "bad"; // okay??? Using 'g' and not 'f'.
 f.s.length = 1; // fails??? changing f's data
 g.s.length = 1; // okay??? Using 'g' and not 'f'.
 f.s[0] = 'r'; // okay ??? not changing f's data

 f = new Foo("rabbit"); // okay 'cos 'f' is a reference
                        // and not the object???

 (*(&f)).a = 1; // okay??? access through f's address and not 'f'.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Justice for David Hicks!"
21/03/2007 10:39:07 AM



More information about the Digitalmars-d mailing list