Extended Type Design.

Walter Bright newshound at digitalmars.com
Sat Mar 17 21:33:16 PDT 2007


Derek Parnell wrote:
>> final
>  This applies only to assignments to Write-Once RAM locations. This can be
> done by either the compiler or at run time depending on the amount of
> knowledge the compiler has about the location's usage.

No. This applies to rebinding of a name:
	final x = 3;
	x = 4;		// error, x is final
i.e. final applies to the declared name, not the type.

>> const
>  This is applied to declarations to prevent code in the same scope as the
> declaration from being able to modify the item being declared.

No. This means that it is a readonly view of data - other views of the 
same data may change it.
	char[] s = "hello".dup;
	const char[] t = s;
	t[0] = 'j';	// error, const char[] is a readonly view
	s[0] = 'j';	// ok
	writefln(t);	// prints "jello"
	t = s[1..2];	// ok, as t is not final
Note that const applies to the type, not the name.

>> invariant
>  This is applied to declarations to prevent code in the same application as
> the declaration from being able to modify the item being declared.

Almost right. It isn't the declaration, but the *type* that is 
invariant. Invariant applies to the type, not the name.

> As you can see, I'm confused as to how the qualifier effects which code is
> allowed to change which items. Even more so when it comes to reference
> items ... 'cos I'm not sure how to use these qualifiers to specify whether
> the reference and/or the data being referenced can be changed, and by whom.

'final' is a storage class, like 'static'. It doesn't apply to the type 
of the symbol, only the symbol itself.
'const' and 'invariant' apply to the type of the symbol, not the symbol.



More information about the Digitalmars-d mailing list