Search code examples
d

Why isn't there a `isIn` trait in D?


I am new to D, so I might get something completely wrong, please enlighten me!

As far as I understand from the documentation D offers traits for finding out about function parameter adornments:

void fooref(ref int x) { 
  static assert(__traits(isRef, x)); 
  static assert(!__traits(isOut, x)); 
  static assert(!__traits(isLazy, x)); 
} 
void fooout(out int x) { 
  static assert(!__traits(isRef, x)); 
  static assert(__traits(isOut, x)); 
  static assert(!__traits(isLazy, x)); 
} 
void foolazy(lazy int x) { 
  static assert(!__traits(isRef, x)); 
  static assert(!__traits(isOut, x));
  static assert(__traits(isLazy, x));
}

Why isn't there a trait for finding out about the in adornment?


Solution

  • __traits and std.traits have lots of good stuff in them, but they're not necessarily complete. So, if you find something that you really think should be inspectable with __traits or std.traits, then please create an enhancement request.

    Now, that being said, it wouldn't really make sense for there to be a way to check for in. in is the same as const scope and if you could check for in, then const scope would have to match it as well. Really, do you care about const or do you care about scope? Or do you care about both? Check for those, not for in.

    However, unfortunately, at the moment, it doesn't actually work to check for scope when in was used. std.traits.ParameterStorageClassTuple can be used to get the storage class of a parameter (none, scope, out, ref, and/or lazy), but it currently fails to give scope for in. So, for the moment, you're out of luck. The bug has been reported though.

    But you can still check for const by doing something like is(T == const) (where T is the type of the parameter that you're checking).