use template function without assignment
    Namespace 
    rswhite4 at googlemail.com
       
    Tue Jul 30 00:12:10 PDT 2013
    
    
  
On Monday, 29 July 2013 at 23:09:20 UTC, JS wrote:
> I have created a template Pragma that emulates pragma but 
> better, the problem is that I have to assign it to something 
> which is very redundant in my code:
>
> enum temp = Pragma!(msg)
>
> e.g.,
>
> template Pragma(alias amsg)
> {
>     string Pragma(string file = __FILE__)
>     {
>         pragma(msg, amsg);
>         return "";
>     }
> }
>
> When I try to use void instead of string and do something like
>
> Pragma!(msg)
>
> I get an error that the template has no effect. It does have an 
> effect but what it is complaining about is exactly what I want.
>
> I've tried all kinds of combinations(mixins work but I then 
> can't ise __FILE__) and nothing works. Maybe someone has an 
> idea.
You could call your template in a static CTor:
----
import std.stdio;
template Pragma(alias amsg)
{
     void Pragma(string file = __FILE__)
     {
         pragma(msg, amsg);
     }
}
static this() {
	Pragma!("foo")();
}
void main()
{
	writeln("Hello world!");
}
----
Maybe this helps?
    
    
More information about the Digitalmars-d-learn
mailing list