member func is best choice for pointers?

Ali Çehreli acehreli at yahoo.com
Fri Apr 7 15:30:37 UTC 2023


On 4/6/23 07:26, a11e99z wrote:
 > ```d
 > import std, core.lifetime;
 >
 > struct Node {
 >      Node* pNext;
 >      void func() { "Node::func %s".writeln( pNext); }

That's a member function, which works with the obj.func() syntax.

However, there is another feature of D that is in play here: Members can 
directly be accessed through pointers without dereferencing.

   pn.func()

has the same effect as

   (*pn).func()

(That's why D does not use C's -> operator).

 > }
 > void func( Node* p ) { "::func %s(%s)".writeln( p, p == null ? p :
 > p.next); }

Ok, that function can be called normally as

   func(pn)

or with UFCS as

   pn.func()

However, UFCS takes place only when there is no such member.

I understand how this can be surprising but it is still within spec: 
There does exist a member with the name 'func', so UFCS is not applied.

One can rightly argue that automatic pointer dereferencing should take 
place after UFCS consideration but apparently that's not the case. If 
that were the case, I suspect there would be other programmers who would 
be looking for the current behavior.

Ali



More information about the Digitalmars-d-learn mailing list