Scope of Mixins

Ali Çehreli acehreli at yahoo.com
Thu Aug 26 18:07:48 UTC 2021


On 8/26/21 10:45 AM, Adam D Ruppe wrote:
> On Thursday, 26 August 2021 at 17:39:16 UTC, Ali Çehreli wrote:
>> String mixins are appealing because they can inject code like C macros 
>> do. It's not trivially possible to do the same with template mixins.
> 
> Template mixins are great, but obviously totally inappropriate here. I'm 
> just talking about using a normal function, possibly with an alias 
> argument, instead of any kind of mixin.
> 
> Too often D programmers reach for fancy code generation when a simpler 
> function is a better fit.

Agreed. Something like the following for the OP:

import std.traits : isPointer;

auto valueFrom(T)(T var)
if (isPointer!(typeof(var))) {
   return *var;
}

auto valueFrom(T)(T var)
if (!isPointer!(typeof(var))) {
   return var;
}

void main() {
   int x;
   int i = 42;
   x = valueFrom(i);

   int * p = &i;
   x = valueFrom(p);
}

In some cases it's more useful to have a 'static if' inside a single 
function template instead of two separate function templates.

Ali



More information about the Digitalmars-d-learn mailing list