member func is best choice for pointers?

a11e99z black80 at bk.ru
Thu Apr 6 14:26:01 UTC 2023


```d
import std, core.lifetime;

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

void main()
{
     Node n;
     auto pn = &n; //cast( Node* )0;
     pn.func(); // prints: Node::func
     // WTF?
     // why called Node::func for Node* when ::func is right 
choice for it?
}
```

problem:
member func has invariant that **this** is not null (programmer 
thinks so).
global func hasn't the one and programmer should check for it 
before doing something.

when pn is null u've got AccessViolation/Signal_11 with no 
stacktrace.

workaround:
avoid overloaded UFCS for pointers


More information about the Digitalmars-d-learn mailing list