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

Reply via email to