ref is unsafe

Carl Sturtivant sturtivant at gmail.com
Sun Dec 30 18:34:18 PST 2012


/*
The implementation of delegates has solved an analogous problem. 
e.g.
*/
import std.stdio;

auto getfun( int x) {
	int y = x*x;
	int ysquared() {
		return y*y;
	}
	return &ysquared;
}

void main() {
	auto f1 = getfun(2);
	auto f2 = getfun(3);
	writeln( f1() );
	writeln( f2() );
}
/*
The variable y no longer exists local to getfun, but its 
existence is prolonged making its use safe.

Just like _immutable_, some clever compiler inference that is 
transitive plus a delegate-like solution may do the job with ref 
without imposing constraints upon the sane. Plus, it may lead to 
new terrain when the liberation of local variables as above 
occurs in this context.

Warning: this is all speculation.
*/


More information about the Digitalmars-d mailing list