Head Const
Nick Treleaven via Digitalmars-d
digitalmars-d at puremagic.com
Tue Feb 16 04:46:11 PST 2016
On Tuesday, 16 February 2016 at 10:32:45 UTC, Timon Gehr wrote:
> struct S{
> int[] a;
> void foo()headconst{
> a[0]=1; // ok
> // arr.length=2 // error
> }
> }
>
> void main(){
> headconst(S) s([0,2,3]);
> s.foo();
> assert(s.a==[1,2,3]);
> }
>
> How to do this in the library?
Maybe make foo a free function:
struct S{
int[] a;
}
void foo(HConst!S self){
self.a[0]=1; // ok
self.arr.length=2 // no effect
}
Haven't tried, but maybe HConst can allow access to fields of S
by value, not ref. That could prevent mutation like C++ const,
but it wouldn't disallow mutation of the temporary copy. Not sure
if that's good enough. I suppose the compiler could warn about
unused temporary mutation.
More information about the Digitalmars-d
mailing list