UFCS assignment syntax for templated function

rcorre via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 1 03:59:39 PST 2016


The following works:

---
struct Foo { float x; }

auto val(Foo foo) { return foo.x; }
auto val(ref Foo foo, float val) { return foo.x = val; }

unittest {
   auto f = Foo();
   f.val = 5;
   assert(f.val == 5);
}
---

But the following fails to compile with 'val(b) is not an lvalue'.

---
struct Bar(T) { T x; }

auto val(T)(Bar!T bar) { return bar.x; }
auto val(T)(ref Bar!T bar, float val) { return bar.x = val; }

unittest {
   auto b = Bar!int();
   b.val = 5;
   assert(b.val == 5);
}
---

Is there a way to invoke a templated function using the UFCS 
assignment syntax?
That is, can `obj.fun = val` be translated to `fun(obj, val)` 
when fun is a templated function?


More information about the Digitalmars-d-learn mailing list