November 16, 2018 Compile time code generation | ||||
---|---|---|---|---|
| ||||
Hi, Is something like this is doable ? ------------------------------------------------------------------- // all compile time MapperGen.Create!(A, B) .Map!("p1", "p2") .Map!("myprop", "otherprop") .Gen(); // The code above must generate something like // // class Mapper { // B map(A value) { // B ret = new B(); // ret.p1 = value.p2; // ret.myprop = p2.otherprop; // return ret; // } // } // runtime use auto b = new Mapper().map(new A()); ------------------------------------------------------------------- I can't figure out how to concatenate string at runtime and the mixin the resulting string?. Thanks in advance ! |
November 16, 2018 Re: Compile time code generation | ||||
---|---|---|---|---|
| ||||
Posted in reply to boolangery | On 11/16/18 12:10 PM, boolangery wrote:
> Hi,
>
> Is something like this is doable ?
>
> -------------------------------------------------------------------
> // all compile time
> MapperGen.Create!(A, B)
> .Map!("p1", "p2")
> .Map!("myprop", "otherprop")
> .Gen();
>
> // The code above must generate something like
> //
> // class Mapper {
> // B map(A value) {
> // B ret = new B();
> // ret.p1 = value.p2;
> // ret.myprop = p2.otherprop;
> // return ret;
> // }
> // }
>
> // runtime use
> auto b = new Mapper().map(new A());
> -------------------------------------------------------------------
>
> I can't figure out how to concatenate string at runtime and the mixin the resulting string?.
I'm not understanding what you are trying to do, but to answer your question, you can only mixin a string generated at compile time.
However, you can concatenate a string at runtime inside a CTFE-able function, and then mixin the result.
Example:
string foo(string[] fields...)
{
string result = "struct Foo {";
foreach(f; fields)
result ~= "int " ~ f ~ ";";
return result ~ "}";
}
mixin(foo()); // generate the struct
-Steve
|
Copyright © 1999-2021 by the D Language Foundation