On Wednesday, 12 April 2017 at 11:06:13 UTC, Juanjo Alvarez wrote:
Hi!

With "alias this" accepting runtime variables I'm struggling to

FYI, you are not talking about "alias this", but "alias template parameters", two very different concepts.

understand the difference between a generic function with an "alias this" parameter and another one with a "runtime" parameter of template type.

Example:

// ---- example code ----
import std.stdio: writeln;

void writevalue1(alias param)() { writeln(param); }

void writevalue2(T)(T param) { writeln(param); }

void main() {
  import std.random: uniform;
  auto someNum = uniform(0, 1000); // runtime value
  writevalue1(someNum);
  someNum = uniform(0, 1000);
  writevalue2(someNum);
}
// ---- example end -----

Since both versions work with runtime values, what's are the differences? When I should prefer one version over the other?

Neither template cares or knows anything about runtime values. When the compiler encounters them, it instantiates an instance of each by creating functions that work with runtime values. But it does so differently for each.

In your instantiation of writevalue1, you are passing the symbol "someNum" as an alias template parameter. This instantiation will only ever be used with "someNum". If you pass it a different symbol, you'll get a separate instantiation, even if it has the same type as "someNum".

In your instantiation of writevalue2, you are getting a function that takes a single argument of `typeof(someNum)`. This instantiation will be used for any value you pass to it that has the same type.

If you're only interested in the value of a variable, you almost certainly want to use template type parameters most of the time, especially if you are going to be calling the function with multiple variables. That way, variables of the same type all have one instantiation and you avoid bloat.

Use alias parameters when you actually care about the *symbol* and not the value.

Reply via email to