www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - UFCS and function scope

reply "ixid" <adamsibson hotmail.com> writes:
Why does UFCS seem to require that functions be defined in a
higher (global being the highest) scope than themselves while the
normal function syntax works fine?

This is OK:
int fun1(int n) {
	return n + 1;
}

int fun2(int n) {
	return n.fun1;
}

This is also OK:
int fun3(int n) {
	int fun4(int n) {
		return n + 1;
	}

	return fun4(n);
}

This is not OK:
int fun5(int n) {
	int fun6(int n) {
		return n + 1;
	}

	return n.fun6;
}
Aug 15 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
ixid:

 This is not OK:
 int fun5(int n) {
 	int fun6(int n) {
 		return n + 1;
 	}

 	return n.fun6;
 }
Unfortunately this is by design, and at the moment it's unlikely to change. And I think it's done this way to avoid troubles with code in class methods. Bye, bearophile
Aug 15 2013