Finding out about D - 101

Steve Teale steve.teale at britseyeview.com
Mon May 11 10:44:42 PDT 2009


Steven Schveighoffer Wrote:

> > This tells me that a string in D is a sequence of characters (whatever  
> > that might mean) in memory, prefixed by a size_t length. Of  course,  
> > that's not to say there is no more to it, but the values for 'xtra'  
> > don't give us much clue. If there was some other member of struct S, I'd  
> > kind of expect to see bit flags there indicating whether the array of  
> > char was invariant, const, or fair game.
> >
> > So I'm left with the question as to how does 'invariant' have teeth?
> 
> invariant is not a runtime flag, but rather a type modifier.  The teeth of  
> invariance only exist during compilation, once you're in runtime, the  
> expectation is that the compiler will have weeded out all your attempts to  
> change the invariant data, so no need to keep track of its invariance.  It  
> is not immune to casting.
> 
> And BTW, string is invariant(char)[], not invariant(char[]), which is a  
> distinct difference.  The former is a mutable array of invariant  
> characters, the latter is an invariant array of invariant characters.  You  
> can't change the length or reassign an invariant(char[]), but you can  
> reassign/append to an invariant(char)[].
> 
> The xtra flags do not exist.  You are printing out garbage data on the  
> stack (most likely the pointer field of ca).
> 
> -Steve

Great, so there are experienced D programmers checking on this newsgroup!

I'd kind of figured out from the test program that the invariant thing was just compile time, but  thanks for pointing out my misconception about the string alias. I should read things more carefully.

- Also Steve



More information about the Digitalmars-d-learn mailing list