Thread overview | |||||
---|---|---|---|---|---|
|
September 15, 2016 Getting a String Literal From an Aliased Template Mixin Parameter | ||||
---|---|---|---|---|
| ||||
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: [code] mixin template internalMockRig(alias _var, string _varName, alias _varStandard, alias _varMock) { version(unittest) { @property bool internalMockSetting() { return _useInternalMocks; } @property useInternalMocks(bool uim) { // Are we changing the mock type? if (uim != _useInternalMocks) { _useInternalMocks = uim; if(_useInternalMocks) { _var = new _varMock; } else { _var = new _varStandard; } } } private: bool _useInternalMocks = false; } } [/code] Then in the object: [code] class usingTestThing { testThing thing; mixin internalMockRig!(thing, testThing, testThingMock); this() { thing = new testThing; } } [/code] 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). |
September 15, 2016 Re: Getting a String Literal From an Aliased Template Mixin Parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to jsako | 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");
}
|
September 15, 2016 Re: Getting a String Literal From an Aliased Template Mixin Parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anonymouse | On Thursday, 15 September 2016 at 01:40:50 UTC, Anonymouse wrote:
> 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");
> }
Aha! Yes, that's exactly what I was looking for! Thanks!
|
Copyright © 1999-2021 by the D Language Foundation