final variables

#ponce aliloko at gmail.com
Mon Sep 21 18:22:57 PDT 2009


Is there a need for final variables now that we have immutable and const in D2 ?


This program does not compile: 
import std.stdio;

 int main(char[][] args)
{
	final int a = 4;
        a = 8;
	writefln("%s", a);
	return 0;
}

But the following program does run: 

import std.stdio;

 int main(char[][] args)
{
	final int a = 4;
	final int* p = &a;
	*p = 8;
	writefln("%s", a);
	return 0;
}

and prints 8. 

So final for variables seems weaker than C const.
Another problem is that final looks like Java :)


More information about the Digitalmars-d-learn mailing list