Extended Type Design.

Derek Parnell derek at nomail.afraid.org
Sun Mar 18 19:49:47 PDT 2007


On Sat, 17 Mar 2007 21:33:16 -0700, Walter Bright wrote:

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

I'm sure its just a terminology problem I'm having, but I still can't
understand what you are trying to tell me. I'm sorry I'm so thick!

To me the word 'type' refers to the data type associated with a symbol -
like 'int', 'float', 'char[]' , etc... So when you say "'const' and
'invariant' apply to the type" it sounds like you are saying that 'const'
prevents a symbol from changing types - which doesn't make sense to me. In
D, a symbol can never change types once it has been declared. 

So, I'm now wondering if by "type" you are actually meaning the data
represented by the symbol and not its data-type. However that doesn't quite
make sense either because that's what I was saying but you disagreed with
me!?

Please bear with me. Oh, and anyone else can chime in to help make it clear
to me too, please.

Let's take 'final' again. 

Walter said:
> 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.

Now by 'binding' I assume you mean 'having the compiler associate a value
with a symbol'.

So "final x = 3" is sort of equivalent to C's "#define x (3)", but in D
there are scope and data-type implications. 

I expect that in D we will also be able to do ...

  final double z = 4.288392;

but what about things like ...

  int y = 3;
  final x = y;
  final q = someFunc();

And is ...

    final s = "qwerty";
    char[] t = s;

identical to ...

    char[] t = "qwerty".dup;


I presume this would fail at compile time ...

    void XYZZY(inout char[] x) { . . . }
    . . .
    final s = "qwerty";
    XYZZY(s);


*****
Okay, now let's revisit 'const'.

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

And you said ...
> 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.

Apart from the last sentence, which still confuses the hell out of me, I
think we almost are saying the same thing.

Using your example code, I was saying that the declaration "const char[] t"
means that you can't change 't' (i.e. t.ptr and t.length cannot be altered)
but you can change the data that t.ptr points to.  However, I see by your
explanation that I got this the wrong way around. I now read you saying
that 'const char[] t' means that program can change t.ptr and/or t.length
but it cannot use 't' to change the data it points to. I said nothing about
getting to the data via another symbol. Also, I see that you are saying
"final const char[] t" means that neither 't' nor its data can be altered.

Is there any difference between "final const char[] t" and "const final
char[] t"?

Is "const int x = 4" and "final int x = 4" mean the same thing (not a
reference type this time)? 

Would "final const x = 5" compile? Would it mean anything different from
just 'final' or just 'const'?

It would seem that 'const' is primarily used to protect referenced data
rather than the reference itself. Is that right?


Will this be allowed ...

   void XYZZY(const char[] s)
   {
      char[] t;
      t = s;   // fails here?
      t[0] = 'a';
   }

*****
And now to 'invariant' ...

You said ...
> It isn't the declaration, but the *type* that is 
> invariant. Invariant applies to the type, not the name.

I have no idea what you mean by that statement! I still can't see that
data-types can change. But maybe you mean data rather than data-type, but
then isn't that what 'const' is doing? And I'm sure you *do not* mean that
somehow you can rename symbols during compilation!? So I can't see what is
"invariant" - as in - what is "not changing".

If I code "invariant char[] t", ...
  (A) are changes to t.ptr/t.length prevented? 

  (B) are changes to the data that 't' points to prevented? 

How is 'invariant' different from 'final'?

How is 'invariant' different from 'const'?

How is 'invariant' different from 'final const'?


Is combining 'invariant' with 'final' and/or 'const' meaningful? If so,
how?

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Justice for David Hicks!"
19/03/2007 11:55:33 AM



More information about the Digitalmars-d mailing list