www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - template const to compile-time function

reply Carlos Santander <csantander619 gmail.com> writes:
Currently I have this template:

template seqWrapper (char[] property, alias seq, alias array)
{
	const seqWrapper = typeof(array).stringof ~ " " ~ property ~ "()"
	"{"
	"if (" ~ array.stringof ~ ".length == 0)"
	"	" ~ array.stringof ~ " = seqToArray (" ~ seq.stringof ~ ");"
	"return " ~ array.stringof ~ ";"
	"}";
}

Which is simply used like this:

mixin (seqWrapper!("users", _list, _users));

And it works. But I was wondering if it was better to have it as a compile-time 
function, and if so, how it could be changed. I'm using D1.0, btw.

-- 
Carlos Santander Bernal
Sep 04 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Carlos Santander wrote:
 Currently I have this template:
 
 template seqWrapper (char[] property, alias seq, alias array)
 {
     const seqWrapper = typeof(array).stringof ~ " " ~ property ~ "()"
     "{"
     "if (" ~ array.stringof ~ ".length == 0)"
     "    " ~ array.stringof ~ " = seqToArray (" ~ seq.stringof ~ ");"
     "return " ~ array.stringof ~ ";"
     "}";
 }
 
 Which is simply used like this:
 
 mixin (seqWrapper!("users", _list, _users));
 
 And it works. But I was wondering if it was better to have it as a
 compile-time function, and if so, how it could be changed. I'm using
 D1.0, btw.
 
string seqWrapper(string property, string seq, string array) { return `typeof(`~array~`) `~property~`() { if( (`~array~`).length == 0 ) `~array~` = seqToArray(`~seq~`); return `~array~`; }`; } mixin (seqWrapper("users","_list","_users")); Six of one, half-dozen of the other, really. Unless you need to do stuff involving loops or string processing, templates are quite sufficient. -- Daniel
Sep 04 2007
parent reply Carlos Santander <csantander619 gmail.com> writes:
Daniel Keep escribió:
 
 string seqWrapper(string property, string seq, string array)
 {
     return `typeof(`~array~`) `~property~`()
     {
         if( (`~array~`).length == 0 )
             `~array~` = seqToArray(`~seq~`);
         return `~array~`;
     }`;
 }
 
 mixin (seqWrapper("users","_list","_users"));
 
I hadn't thought of that first line with typeof. Thanks.
 Six of one, half-dozen of the other, really.  Unless you need to do
 stuff involving loops or string processing, templates are quite sufficient.
 
 	-- Daniel
I was thinking about code-generation bloat, but I guess I'd have to measure that myself. -- Carlos Santander Bernal
Sep 04 2007
parent reply Jari-Matti =?ISO-8859-1?Q?M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
Carlos Santander wrote:

 I was thinking about code-generation bloat, but I guess I'd have to
 measure that myself.
If you only use the wrapper function in CTFE, try moving it to a separate module and not linking it, just importing. That way no unnecessary code is generated to the final executable or object files.
Sep 05 2007
parent Carlos Santander <csantander619 gmail.com> writes:
Jari-Matti Mäkelä escribió:
 Carlos Santander wrote:
 
 I was thinking about code-generation bloat, but I guess I'd have to
 measure that myself.
If you only use the wrapper function in CTFE, try moving it to a separate module and not linking it, just importing. That way no unnecessary code is generated to the final executable or object files.
Nice tip. Thanks. -- Carlos Santander Bernal
Sep 05 2007