Add compile time mutable variable type
Chang Long
changlon at gmail.com
Mon Jun 4 18:01:06 PDT 2012
I need a type can be change on compile time, it can be
immutable or mutable at run time.
For example :
-------------------------------------------------------------------------------
abstract class Template_parameter_Base{
TypeInto ti;
string file ;
size_t line l;
string name ;
this(TypeInto ti, string file, size_t line, string name){
this.ti = ti ;
this.file = file ;
this.line = line ;
this.name = name ;
}
}
class Template_parameter(T) : Template_parameter_Base {
T value ;
alias T this;
void assign(ref T t){
value = t ;
}
}
class Template {
static
compile_time_mutable(string[Template_parameter_Base])
template_vars ;
void assign(string name, string __file__ = __FILE__, size_t
__line__ =__LINE__, T)(T t) {
static if( name in template_vars ){
static parameter = template_vars[name] ;
} else {
static parameter = template_vars[name] = new
Template_parameter(typeid(T), __file__, __line__ );
}
parameter.assign(t);
}
string render(){
// use template_vars comiplate a dynamic link lib , and load
it, and put the template_vars to it, and run it ,
// if dynamic link lib already exists, or the templte file
is not changed, just run it .
}
}
void main(){
static tpl = new Template;
auto is_login = false ;
tpl.assign!"is_login"( is_login) ) ;
if( is_login ) {
tpl.assign!"user"( new User() ) ;
}
}
-------------------------------------------------------------------------------
Why I need this is because I need to know all parameters at first
time call Template.render, but a lot parameter will not be call
at run time. so I need a type can be changed at compile time.
the new type is just like mutable type at CTFE.
More information about the Digitalmars-d
mailing list