www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - UFCS assignment syntax for templated function

reply rcorre <ryan rcorre.net> writes:
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?
Jan 01
parent rcorre <ryan rcorre.net> writes:
On Friday, 1 January 2016 at 11:59:39 UTC, rcorre wrote:
 auto val(T)(ref Bar!T bar, float val) { return bar.x = val; }
Uh, never mind. That `float` should have been `T`. Seems to work now.
Jan 01