Created
October 3, 2023 20:40
-
-
Save run-dlang/fe6d9c381b037e7ecc9c0898fca69e38 to your computer and use it in GitHub Desktop.
Code shared from run.dlang.io.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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