Gneric linkedList range adaptor

Ben Jones fake at fake.fake
Fri Feb 10 22:10:20 UTC 2023


I'm trying to write a range adaptor for linked list types.  The 
range type seems to work OK, but my helper function to deduce the 
node type has a compiler error.  My hunch is that `nextField` 
loses its association with T when I'm trying to pass it as a 
template parameter inside the helper method, but I can't use 
__traits(child) there to fix it.

Any idea how to fix the helper method?

Is this something that would be useful to add to std.range 
somewhere?  Or is something like it already in Phobos?

Thanks

```
import std.stdio;
import std.range;

struct LinkedListAdaptor(alias nextField, T){
  	T current;
     @safe:
    	nothrow:

     this(T head){
      	current = head;
     }

     bool empty() const {
     	return current == null;
     }	

     T front() {
      	return current;
     }

     void popFront() {
		current = __traits(child, current, nextField);
     }
}

auto linkedListAdaptor(alias nextField, T)(T head) if(is(T == U*, 
U)){
  	return LinkedListAdaptor!(nextField, T)(head);

     //fails with:
     //onlineapp.d(27): Error: symbol or expression expected as 
first argument of __traits `child` instead of `Node*`
     // return LinkedListAdaptor!(__traits(child, T, nextField), 
T)(head);
}

struct Node{
  	int data;
     Node* next;
}

void main(){
	Node* head = new Node(10, new Node(20, null));

     auto rng1 = LinkedListAdaptor!(Node.next, Node*)(head);
     auto rng = linkedListAdaptor!(Node.next)(head);
     foreach(const x; rng){
         writeln(x.data);
     }
}
```

This fails with:

Error: need `this` for `linkedListAdaptor` of type `pure nothrow 
@nogc @safe LinkedListAdaptor!(next, Node*)(Node* head)`


More information about the Digitalmars-d-learn mailing list