On 07/27/2017 02:16 PM, Chris wrote:

> What is the value of `???` in the following program:

> void categorize(??? toks) {
>   foreach (t; toks) {
>     writeln(t);
>   }
> }

The easiest solution is to make it a template (R is a suitable template variable name for a range type):

void categorize(R)(R toks) {
  foreach (t; toks) {
    writeln(t);
  }
}

Your function will work with any type that can be iterated with foreach and can be passed to writeln. However, you can use template constraints to limit its usage, document its usage, or produce better compilation errors when it's called with an incompatible type (the error message would point at the call site as opposed to the body of categorize):

import std.range;
import std.traits;

void categorize(R)(R toks)
if (isInputRange!R && isSomeString!(ElementType!R)) {
  foreach (t; toks) {
    writeln(t);
  }
}

Ali

Reply via email to