digitalmars.D - compile-time function in a class?
- Davidl (27/27) Mar 20 2007 static keyword is already used by class.
- Tyler Knott (7/7) Mar 20 2007 You don't need to mark compile-time functions as static. Functions are ...
- Charlie (14/23) Mar 20 2007 are only evaluated at compile-time when they can't possibly be evaluated...
- janderson (3/34) Mar 20 2007 One way to test if a function works at compile time would be to wrap it
- Davidl (14/14) Mar 20 2007 class test
- Charlie (4/21) Mar 20 2007 I don't think you can instantiate a class at compile time, which is what...
- Alexander Panek (4/21) Mar 20 2007 AFAIK, to be able to mixin a function, it has to be either inside a
- Davidl (15/33) Mar 20 2007 class test
static keyword is already used by class. and i hope static static compiletimefunc() { } can be recognized as a compiletime func all i want is the name space protection from my class. like if i want to do some code injection for my certain class attributes. like class abc { mixin(compiletimefunc(`membera,memberb`)); //pusedo code begines: static static compiletimefunc(char[]members) { char []result; //parse members to membernames result~=3D`private:int _m_`~membernames~`;`; result~=3D`public:int member(){return _m_`~membernames~`}`; //g= etter // loop return result; } } actually the compiletimefunc only used by the class abc, but now i can d= o is make it outside the class and polute the name space.
Mar 20 2007
You don't need to mark compile-time functions as static. Functions are only evaluated at compile-time when they can't possibly be evaluated at run-time (such as in mixin declarations/statements/expressions or as initializers of static or global variables). E.g.: char[] exampleFunc() { return "example"; } char[] example = exampleFunc(); //This is global, so exampleFunc is executed at compile-time void func() { char[] example = exampleFunc(); //Not global or static, so run-time }
Mar 20 2007
are only evaluated at compile-time when they can't possibly be evaluated at run-time (such as in mixin declarations/statements/expressions or as initializers of static or global variables). E.g.: I love compile time functions, but saying 'are only evaluated when they cant be at run-time' leaves allot to be desired in terms of ease of use. How do i know for sure its evaluated at run time ? I could use a pragma , but it seems like we have to follow a very thin line to get there, it would be extremely nice if we had someway of explicitly saying 'call this at compile time'. exampleFunc(); maybe ? That way instead of guessing we're doing it at compile-time, we could be certain, and also let the programmer know whats going on , instead of him having to trace the same thin line. Charlie Tyler Knott wrote:You don't need to mark compile-time functions as static. FunctionsYou don't need to mark compile-time functions as static.char[] example = exampleFunc(); //This is global, so exampleFunc is executed at compile-time void func() { char[] example = exampleFunc(); //Not global or static, so run-time }
Mar 20 2007
Charlie wrote:>> You don't need to mark compile-time functions as static. Functions are only evaluated at compile-time when they can't possibly be evaluated at run-time (such as in mixin declarations/statements/expressions or as initializers of static or global variables). E.g.: I love compile time functions, but saying 'are only evaluated when they cant be at run-time' leaves allot to be desired in terms of ease of use. How do i know for sure its evaluated at run time ? I could use a pragma , but it seems like we have to follow a very thin line to get there, it would be extremely nice if we had someway of explicitly saying 'call this at compile time'. exampleFunc(); maybe ? That way instead of guessing we're doing it at compile-time, we could be certain, and also let the programmer know whats going on , instead of him having to trace the same thin line. Charlie Tyler Knott wrote:One way to test if a function works at compile time would be to wrap it in a mixin.You don't need to mark compile-time functions as static.char[] example = exampleFunc(); //This is global, so exampleFunc is executed at compile-time void func() { char[] example = exampleFunc(); //Not global or static, so run-time }
Mar 20 2007
janderson wrote:Charlie wrote:To be explicit, I mean the function call. const int X = mixin(foo());>> You don't need to mark compile-time functions as static. Functions are only evaluated at compile-time when they can't possibly be evaluated at run-time (such as in mixin declarations/statements/expressions or as initializers of static or global variables). E.g.: I love compile time functions, but saying 'are only evaluated when they cant be at run-time' leaves allot to be desired in terms of ease of use. How do i know for sure its evaluated at run time ? I could use a pragma , but it seems like we have to follow a very thin line to get there, it would be extremely nice if we had someway of explicitly saying 'call this at compile time'. exampleFunc(); maybe ? That way instead of guessing we're doing it at compile-time, we could be certain, and also let the programmer know whats going on , instead of him having to trace the same thin line. Charlie Tyler Knott wrote:One way to test if a function works at compile time would be to wrap it in a mixin.You don't need to mark compile-time functions as static.char[] example = exampleFunc(); //This is global, so exampleFunc is executed at compile-time void func() { char[] example = exampleFunc(); //Not global or static, so run-time }
Mar 20 2007
That works well for functions that mixin code, but what if you just want to return an integer ? janderson wrote:janderson wrote:Charlie wrote:To be explicit, I mean the function call. const int X = mixin(foo());>> You don't need to mark compile-time functions as static. Functions are only evaluated at compile-time when they can't possibly be evaluated at run-time (such as in mixin declarations/statements/expressions or as initializers of static or global variables). E.g.: I love compile time functions, but saying 'are only evaluated when they cant be at run-time' leaves allot to be desired in terms of ease of use. How do i know for sure its evaluated at run time ? I could use a pragma , but it seems like we have to follow a very thin line to get there, it would be extremely nice if we had someway of explicitly saying 'call this at compile time'. exampleFunc(); maybe ? That way instead of guessing we're doing it at compile-time, we could be certain, and also let the programmer know whats going on , instead of him having to trace the same thin line. Charlie Tyler Knott wrote:One way to test if a function works at compile time would be to wrap it in a mixin.You don't need to mark compile-time functions as static.char[] example = exampleFunc(); //This is global, so exampleFunc is executed at compile-time void func() { char[] example = exampleFunc(); //Not global or static, so run-time }
Mar 20 2007
class test { static char[] c(){return "a";} int a; class b { mixin(c()); } } void main() { } don't u think my example should compile? i don't violate any thing from evaluating func c from compile-time
Mar 20 2007
Davidl wrote:class test { static char[] c(){return "a";} int a; class b { mixin(c()); } } void main() { } don't u think my example should compile? i don't violate any thing from evaluating func c from compile-timeI don't think you can instantiate a class at compile time, which is what you would need to do. If all you want to do is mixin variables i suggest make a template and mixin it in.
Mar 20 2007
Davidl wrote:class test { static char[] c(){return "a";} int a; class b { mixin(c()); } } void main() { } don't u think my example should compile? i don't violate any thing from evaluating func c from compile-timeAFAIK, to be able to mixin a function, it has to be either inside a template, a templated struct, a templated class or just be a template function.
Mar 20 2007
class test { template c(){ static char[] c(){return "int k;";}} int a; class b { mixin(c()); } } void main() { } above is a great work around for me, i get name space protect my compile time func, thx for ur hint of what actually compile-time func could beDavidl wrote:class test { static char[] c(){return "a";} int a; class b { mixin(c()); } } void main() { } don't u think my example should compile? i don't violate any thing from evaluating func c from compile-timeAFAIK, to be able to mixin a function, it has to be either inside a template, a templated struct, a templated class or just be a template function.
Mar 20 2007