Getting a String Literal From an Aliased Template Mixin Parameter
Anonymouse via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Sep 14 18:40:50 PDT 2016
On Thursday, 15 September 2016 at 00:15:42 UTC, jsako wrote:
> I was making a quick mocking infrastructure for internal mocks
> so I wouldn't have to pass in objects to constructors all the
> time and came up with this:
>
> [...]
>
> This works great as long as there is only one mock object. I
> thought about using string mixins to generate the properties on
> a per-variable basis, but the problem is that I can't figure
> out how to get the string of the variable name from the aliased
> template mixin parameter. Is there a way to do this?
>
> If not, I'll have to pass in the strings of the variables
> seperately (as in: mixin internalMockRig!(thing, "thing",
> testThing, "testThing", testThingMock, "testThingMock"); ), but
> that seems inelegant (and a bit error prone).
You mean, the literal string name of the original symbol you
passed as an alias? If so then you want the .stringof property.
void main () {
int first;
bool second;
string third;
mixin Foo!(first, second, third);
}
mixin template Foo(alias abc, alias def, alias ghi)
{
static assert(abc.stringof == "first");
static assert(def.stringof == "second");
static assert(ghi.stringof == "third");
}
More information about the Digitalmars-d-learn
mailing list