Define methods using templates
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Dec 30 05:52:04 PST 2014
On 12/30/14 8:48 AM, Steven Schveighoffer wrote:
> I think it has to do with the fact that when you are defining the
> aliases, m_pos for example, is an *instance* member so requires an
> instance to get an alias.
>
> What you are probably better off doing is:
>
> void SetProperty(Tin, string Field)(ref Tin param) @property pure @safe
> {
> mixin(Field ~ " = param;");
> m_matrixCalculated = false;
> }
>
> alias pos = SetProperty!(float[], "m_pos");
>
> I would also put some strict template constraints on the Field string
> too, because one abuse SetProperty pretty easily there.
A possibly more elegant solution, use opDispatch:
void opDispatch(string Field, Tin)(ref Tin param) @property pure @safe
if(Field == "pos" || Field == "ratio" || ...)
{
mixin("m_" ~ Field ~ " = param;");
m_matrixCalculated = false;
}
Not sure if opDispatch works as a @property this way...
-Steve
More information about the Digitalmars-d-learn
mailing list