why no statements inside mixin teplates?

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 27 13:31:08 PDT 2017


On Friday, 12 May 2017 at 00:20:13 UTC, سليمان السهمي (Soulaïman 
Sahmi) wrote:
> Is there a rational behind not allowing statements inside mixin 
> templates? I know mixin does accept code containing statements, 
> but using mixin is much uglier. so  I was wondering.
>
> example use case:
> //---------------------------------
> int compute(string)
> {
>     return 1;
> }
>
> mixin template testBoilerPlate(alias arg, alias expected)
> {
>     {
>         import std.format : format;
>         auto got = compute(arg);
>         assert(got == expected, "expected %s got 
> %s".format(expected, got));
>     }
> }
>
> unittest
> {
>     mixin testBoilerPlate("12345", 1);
>     mixin testBoilerPlate("00" ~ "0", 2 - 1);
> }
> //--------------------------------

If you can put up with the limitation of what can be done in a 
nested function then this convention works (choose whatever names 
you want, A and __ are just for example):

mixin template A()
{
	auto __()
	{
		++a;
	}
}

void main()
{
	int a = 0;

	mixin A!() __; __.__;

	assert (a == 1);
}



More information about the Digitalmars-d mailing list