On Thursday, 6 April 2023 at 14:26:01 UTC, a11e99z wrote:
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 ...

I understand what you mean. When you try to access the last node of a linked list, you will get the segmentation fault if you are using a built-in viewer (toString). For example:

```d
struct Node
{
  int item;
  Node * back;

  string toString()
  {
    import std.format : format;

    return format("Node::%s (back)-> %s", item,
                                     back.item);
  }
}

void main()
{
  import std.stdio : writeln;

  auto node1 = Node(41);
  auto node1Ptr = &node1;

  //printNode(node1Ptr);/* NOT COMPILE!
  node1.writeln;//* Main::41 (back)-> null */

  auto node2 = Node(42);
  node2.back = node1Ptr;

  printNode(&node2); // Main::42 (back)-> 41
  node2.writeln;     // Node::42 (back)-> 41
}

void printNode(Node * p)
{
  import std.stdio : writefln;

  if(p.back is null)
  {
    writefln("Main::%s (back)-> null", p.item);
  } else {
    writefln("Main::%s (back)-> %s", p.item,
                                p.back.item);
  }
}
```

If you don't remove the comment line (just remove the // sign: toggle-comment) the above code will not compile. Because it is not connect to any node.

@SDB

Reply via email to