On Saturday, 14 January 2023 at 23:07:13 UTC, TheZipCreator wrote:
>This is not the purpose mixin templates are meant to serve. They're for copying declarations into scopes (and as such only support declarations in them). Instead, I think what you want is
I'm trying to understand you. But I regret to say that I do not agree with you. You are a programmer like me, so you don't like this usage?
template MyContainer(string varS = "")
{
struct Var
{
import std.variant;
private
Variant[string] values; alias
values this;
@property {
Variant opDispatch(string name)() const
{
return values[name];
}
void opDispatch(string name, T)(T val)
{
values[name] = val;
}
}
}
static if(varS.length > 0)
{
import std.format;
mixin(varS.format!"Var %s;");
} else
Var data;
}
void main()
{
//otherTest("test ok");/*
mixin MyContainer!"date";
enum Tarih { AY = 1, YIL = 2023 }
date.month = cast(ubyte)Tarih.AY;
date.month.write("/");
assert(date["month"] != Tarih.AY); // because :
assert(date["month"].type == typeid(ubyte));
date.year = cast(short)Tarih.YIL;
date.year.writeln(" in Turkish format");
assert(date["year"] != Tarih.YIL); // because :
assert(date["year"].type == typeid(short));
writefln("Date: %s/%s", date.year, date.month);//*/
} /* Prints:
1/2023 in Turkish format
Date: 2023/1
//*/
import std.stdio;
void otherTest(string str)
{
mixin MyContainer;
data.test = str;
data.test.writeln;
}
I'm 44 and maybe an old-fashioned person who likes macros. But mixins are all kinds of beautiful yaw 😀
Because they can be used as easily as importing. Moreover, they can be personalized.
SDB@79