Template bug with second mixin?
Andre via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Dec 5 03:11:14 PST 2014
thanks a lot. That makes sense.
Kind regards
André
On Friday, 5 December 2014 at 09:29:21 UTC, ketmar via
Digitalmars-d-learn wrote:
> On Fri, 05 Dec 2014 09:19:27 +0000
> Andre via Digitalmars-d-learn
> <digitalmars-d-learn at puremagic.com> wrote:
>
>> I think I mimized the coding too much.
>> What I actually want to achieve is to generate read methods
>> in the structure for different data types (Ubyte, short, int):
>>
>> I thought the eponymous trick would insert the content of the
>> enum instead the name itself?
>>
>> string getReadMethods(string methodName, string dataType)
>> {
>> return dataType~` `~methodName~`(size_t idx) {
>> return v_arr[idx].get!`~dataType~`; }`;
>> }
>>
>> template insertReadMethods(string MethodName, DataType)
>> {
>> enum insertReadMethods = getReadMethods(MethodName,
>> DataType.stringof);
>> }
>>
>> struct Data
>> {
>> mixin insertReadMethods!("readTinyInt", ubyte);
>> mixin insertReadMethods!("readShortInt", short);
>> }
>>
>> void main(){}
>
> you musunderstood how mixin templates works. mixin templates
> are more
> like macroses than templates per se, so you don't need to assign
> anything to enum to get the result. i.e.
>
>
> string getReadMethods(string methodName, string dataType)
> {
> return dataType~` `~methodName~`(size_t idx) {
> return v_arr[idx].get!`~dataType~`; }`;
> }
>
> template insertReadMethods(string MethodName, DataType)
> {
> /*enum insertReadMethods =*/ // no need to do this
> // do that instead ;-)
> mixin(getReadMethods(MethodName, DataType.stringof));
> }
>
> struct Data
> {
> mixin insertReadMethods!("readTinyInt", ubyte);
> mixin insertReadMethods!("readShortInt", short);
> }
>
>
> think about mixin templates as macro definitions which will be
> just
> inserted where you instantiated them.
More information about the Digitalmars-d-learn
mailing list