another idea for compile time functions
janderson
askme at me.com
Thu Feb 8 22:01:12 PST 2007
Hasan Aljudy wrote:
> I think templates is the wrong way to go for compile-time meta-programming.
>
> There's been some talk recently about executing functions at
> compile-time by some people, because these people (including me) feel
> that template recursion and tricks are not the appropriate way to
> manipulate/parse strings for specialized DSL (domain specific languages).
>
> I will give you some ideas I have, and I would like to know your
> thoughts about them.
>
> To me, compile time functions should employ the same concepts behind
> "constant folding": if an expression is composed of compile time
> constants, the compiler will compute the expression at compile time and
> replace it with the result.
>
> # int x = 2 + 3;
> will be optimized by the compiler to:
> # int x = 5;
> Here, the compiler computed the expression "2+3" at compile time, and
> replaced the expression with its result, "5".
>
> The same concept should be applicable to compile time functions. A
> function is a complicated set of expressions.
>
> Some functions are trivial; they take a set of parameters, do something
> with them, without calling or depending on other functions, and return
> the result.
> A "trivial" example:
> # int add( int x, int y ) { return x + y; }
> If they take constant arguments, such functions can surely be computed
> at compile time without much hassle. Such that a call to
> # int Q = add( 10, 12 );
> can be replaced, at compile time, with this:
> # int Q = 22;
>
> The trick may lie in letting the compiler recognize these kind of
> functions. A simple solution might me to come up with a new attribute;
> let's call it "meta":
> # meta int add( int x, int y ) { return x + y; }
> This attribute will assist the compiler in recognizing that this
> function can be computed at compile time if it's given constant arguments.
>
> Now, this may not be very useful because of the restriction that meta
> functions must not call other functions, which is very limiting; so we
> need to extend it.
>
> If a function takes constant arguments and does call other functions,
> then it can only be executed at compile time if all of these other
> functions are meta functions.
>
> example:
> # void subtract( int x, int y ) { return add( x, -y ); }
> This function calls "add", but add is a meta function that can be
> executed at compile time, and will be replaced at compile time with:
> # void subtract( int x, int y ) { return x - y ); }
> So this type of function can definitely be executed at compile time, and
> thus deserves to be a meta function.
>
> # meta void subtract( int x, int y ) { return add( x, -y ); }
>
> The same idea could be extended to string-manipulating functions.
>
> Using these concepts as basis for compile-time meta-programming might
> give us another benefit: we don't have to write compile-time functions
> that repeat the same tasks already done by run-time functions (regex for
> example); we just have to be careful and design our functions so that
> they can be meta functions.
>
> The idea is that if you call a meta function with non-constant
> arguments, the compiler shouldn't complain; it will just treat the
> function call in this case as a regular function call to a runtime
> function.
>
> What do you think about this?! Please tell me your opinions.
This method has been mentioned a few times now ("extention", "static",
"plugin" ect...). If so many people keep coming up with the same idea,
there must be something going for it.
I agree this is probably the best way to go. I should add (and I've
already said this) things marked with "what-ever-we call this" would be
evaluated a compile time so to ensure their compiletime-ness.
-Joel
More information about the Digitalmars-d
mailing list