www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - UFCS doesn't work in some cases

reply "Danyal Zia" <catofdanyal yahoo.com> writes:
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
Sep 09 2014
next sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Tue, 09 Sep 2014 17:58:14 +0000
Danyal Zia via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 Case # 1: When the functions for UFCS are defined inside main=20
 scope
UFCS is not working for nested functions. this is not a bug, it was designed this way. the same for 'with', i believe.
 oversights/bugs? Or is their any rationale behind the decision to=20
 not implement UFCS for them?
there is rationale. see http://dlang.org/function.html, UFCS section.
Sep 09 2014
parent reply "Danyal Zia" <catofdanyal yahoo.com> writes:
On Tuesday, 9 September 2014 at 18:46:31 UTC, ketmar via 
Digitalmars-d-learn wrote:
 UFCS is not working for nested functions. this is not a bug, it 
 was
 designed this way. the same for 'with', i believe.
Apparently it is a bug that UFCS doesn't work with 'with' statement. https://issues.dlang.org/show_bug.cgi?id=10349
 there is rationale. see http://dlang.org/function.html, UFCS 
 section.
Must have missed it before, thanks.
Sep 10 2014
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Wed, 10 Sep 2014 13:58:18 +0000
Danyal Zia via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 Apparently it is a bug that UFCS doesn't work with 'with'=20
 statement.
my fault, i thought that it shouldn't. i'm still not Guru. too bad, will work. ;-)
Sep 10 2014
prev sibling parent "Mathias LANG" <no spam.please> writes:
On Tuesday, 9 September 2014 at 17:58:16 UTC, Danyal Zia wrote:
 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
 The reason why local symbols are not considered by UFCS, is to 
 avoid unexpected name conflicts.
Stated at the very bottom of: http://dlang.org/function.html
Sep 09 2014