preparing for const, final, and invariant

torhu fake at address.dude
Sun May 20 10:36:30 PDT 2007


Walter Bright wrote:
> This is coming for the D 2.0 beta, and it will need some source code 
> changes. Specifically, for function parameters that are arrays or 
> pointers, start using 'in' for them.
> 
> 'in' will mean 'scope const final', which means:
> 
> final - the parameter will not be reassigned within the function
> const - the function will not attempt to change the contents of what is 
> referred to
> scope - the function will not keep a reference to the parameter's data 
> that will persist beyond the scope of the function
> 
> For example:
> 
> int[] g;
> 
> void foo(in int[] a)
> {
>      a = [1,2];	// error, a is final
>      a[1] = 2;   // error, a is const
>      g = a;	// error, a is scope
> }
> 
> Do not use 'in' if you wish to do any of these operations on a 
> parameter. Using 'in' has no useful effect on D 1.0 code, so it'll be 
> backwards compatible.
> 
> Adding in all those 'in's is tedious, as I'm finding out :-(, but I 
> think the results will be worth the effort.

If you've got a C library with a header file containing this:

// C header file
void f(const char* p);

Is there any reason why you should think twice before turning it into 
this D code, and link it with the C library, not knowing anything about 
the implementation of f?

// D import module
extern (C) void f(in char* p);


Without reading the docs for f, would it be better to just go with 
'const'?  In that case it won't be backwards compatible with D 1.0 
anymore.  If I get the meaning of 'scope' correctly, that's the one that 
can cause problems here.



More information about the Digitalmars-d-announce mailing list