Return in finally

Ary Manzana ary at esperanto.org.ar
Wed Mar 7 16:57:35 PST 2007


Frank Benoit (keinfarbton) escribió:
> Any idea, how to simulate
> 2.) make a return from within a finally
> ??

Maybe you can do something like this:

Java
-------------------------------------------------
int func() {
	try {
	} finally {
		return 1;
	}
}
-------------------------------------------------

D
-------------------------------------------------
import std.boxer;

int func() {
	Box finallyReturn;
	try {
	} finally {
		finallyReturn = box(1);
	}
	if (finallyReturn.type !is null) {
		return unbox!(int)(finallyReturn);
	}
}
-------------------------------------------------

You have to be careful, though. After every block that has a return 
statement you have to proceed only if the box's type is null. For example:

Java
-------------------------------------------------
int func() {
	try {
	} finally {
		if (someCondition) {
			return 1;
		}
		doSomeStuff();
		return 2;
	}
}
-------------------------------------------------

D
-------------------------------------------------
import std.boxer;

int func() {
	Box finallyReturn;
	try {
	} finally {
		if (someCondition) {
			finallyReturn = box(1);
		}
		if (finallyReturn.type is null) {
			doSomeStuff();
			finallyReturn = box(2);
		}
	}
	if (finallyReturn.type !is null) {
		return unbox!(int)(finallyReturn);
	}
}
-------------------------------------------------

Does this work?



More information about the Digitalmars-d mailing list