UFCS with implicit "this" ?
Jonathan M Davis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Aug 8 22:33:09 PDT 2016
On Tuesday, August 09, 2016 05:13:44 cy via Digitalmars-d-learn wrote:
> I really like UFCS, which is to say, defining functions outside
> the class/struct to operate on it, but you can still say
> object.function(...) and it'll get rewritten into
> function(object,...).
>
> Only sticky point is the convenience of "this". Like I can go
>
> struct A {
> bool a;
> bool b;
> bool c;
> bool d;
> bool foo() {
> return a && b || c && !d;
> }
> }
>
> But if I try do do the same thing with "bool bar(A object)" I end
> up with this:
>
> bool bar(A object) {
> return object.a && object.b || object.c && !object.d;
> }
>
> My example is a bit contrived, but it occurred to me how neat it
> would be if we could just specify "implicit" objects in our
> current scope.
Personally, I think that you should just make it a member function if it's
not a generic function, but to each their own, I suppose. Regardless,
there's already a feature to do what you're looking for - with statements.
e.g.
bool bar(A object)
{
with(object)
return a && b || c && !d;
}
https://dlang.org/spec/statement.html#WithStatement
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list