is there any reason UFCS can't be used with 'new'?

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Nov 10 11:29:33 PST 2014


On Mon, 10 Nov 2014 19:07:38 +0000
Suliman via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
wrote:

> /// How to use UFCS here?
> userName.name.sayHello();
> ///
you can't.

a little explanation: UFCS substitutes only the first argument. and for
class methods first argument is hidden 'this'. so you can't do it in
this way, you have to write `userName.sayHello(name);` here.

on the very low level calling class methods looks like this:

  // what you see:
  myobj.method(arg);

  // what compiler does:
  // method(myobj, arg);

so in your example compiler sees this:

  sayHello(userName.name);

which is obviously not what you want, 'cause you need this:

  sayHello(userName, name);

or, taking it the easier way: UFCS is for *functions*, not for
*methods*. that's why it "uniform *function* call syntax". it's not
expected to work with methods, as the name suggests. maybe it will be
easier to remember this way, rather than diving into compiler code
transformations. ;-)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141110/4fa213fd/attachment.sig>


More information about the Digitalmars-d-learn mailing list