UFCS doesn't work in some cases

Danyal Zia via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 9 10:58:14 PDT 2014


Hi,

I don't know if this has been mentioned before, but I found two 
strange cases where UFCS doesn't work.

Case # 1: When the functions for UFCS are defined inside main 
scope

class Rect {
	int x = 20;
	int y = 20;
}

void main() {
     import std.stdio : writeln;

     int area(Rect rect) {
		return rect.x * rect.y;
	}

     auto rect = new Rect;

     rect.area.writeln; // Error: no property 'area' for type 
'main.Rect'
}

Put that 'area' definition outside the main body and it works 
fine.

Case # 2: When using UFCS in 'with' scope

class Rect {
	int x = 20;
	int y = 20;
}

int area(Rect rect) {
	return rect.x * rect.y;
}
	
void main() {
     import std.stdio : writeln;

     auto rect = new Rect;

     with(rect) {
		area.writeln; // Error: function main.area (Rect rect) is not 
callable using argument types ()
	}
}

As far as I know, UFCS are designed to make it easy to extend the 
class for specific applications, however, without the ability to 
use UFCS for those cases, it limits its usefulness. Are these 
oversights/bugs? Or is their any rationale behind the decision to 
not implement UFCS for them?

Danyal


More information about the Digitalmars-d-learn mailing list