Template bug with second mixin?

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Dec 5 01:29:11 PST 2014


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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141205/86b7ec03/attachment.sig>


More information about the Digitalmars-d-learn mailing list