Skip to content

Instantly share code, notes, and snippets.

@run-dlang
Created October 3, 2023 20:40
Show Gist options
  • Save run-dlang/fe6d9c381b037e7ecc9c0898fca69e38 to your computer and use it in GitHub Desktop.
Save run-dlang/fe6d9c381b037e7ecc9c0898fca69e38 to your computer and use it in GitHub Desktop.
Code shared from run.dlang.io.
private
{
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.traits;
auto propertyGenerator(string name, string type) {
return format(
`
private %1$s _%2$s;
void %2$s(%1$s value)
{
_%2$s = value;
}
`,
type, name.toLower
);
}
template addProperties(string[] props)
{
const dchar[] addProperties = props
.chunks(2)
.map!(p => propertyGenerator(p[0], p[1]))
.joiner("\n")
.array;
}
enum props = cast(string[]) [
"Width", "bool",
"Height", "int",
];
class Test {
mixin(addProperties!props);
}
}
void main()
{
writeln(propertyGenerator("Dimension", "bool"));
writeln(addProperties!(props));
auto p = new Test;
p.width(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment