import std.conv, std.exception, std.stdio, std.typecons; alias Tuple!(uint, "id") Link; struct Node { uint id; Link[] links; void addLink(uint l) { links ~= Link(l); } immutable(Node) idup() pure const @property { auto linkCopy = to!(Link[])(links); immutable ilinks = assumeUnique(linkCopy); return immutable(Node)(id, ilinks); } } struct Network { Node[uint] nodes; void add(uint i, uint j) { if((i in nodes) is null) nodes[i] = Node(i); if((j in nodes) is null) nodes[j] = Node(j); nodes[i].addLink(j); nodes[j].addLink(i); } // Won't compile immutable(Network) idup() pure const @property { auto nodeCopy = to!(Node[uint])(nodes); immutable imnodes = assumeUnique(nodeCopy); return immutable(Network)(imnodes); } void print() { foreach(k; nodes.keys) { write("[", k, "]"); foreach(l; nodes[k].links) write(" ", l.id); writeln(); } writeln(); } } void main() { auto n1 = Node(1); n1.addLink(5); n1.addLink(6); writeln(n1.links); immutable n2 = n1.idup; writeln(n2.links); Network net1; net1.add(1, 7); net1.add(1, 8); net1.add(2, 9); net1.print(); }