www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - example for compile time function execution

reply Walter Bright <newshound1 digitalmars.com> writes:
I keep racking my brain trying to come up with a short, pithy example of 
CTFE that uses AAs or struct literals. It should be just a few lines, 
and make it clear why CTFE is a great thing. I want to use it in 
presentations.

Any ideas?
Apr 27 2007
next sibling parent Dan <murpsoft hotmail.com> writes:
Walter Bright Wrote:

 I keep racking my brain trying to come up with a short, pithy example of 
 CTFE that uses AAs or struct literals. It should be just a few lines, 
 and make it clear why CTFE is a great thing. I want to use it in 
 presentations.
 
 Any ideas?
You could technically write a D compile-time-executed ECMAScript engine using CTFE and associative arrays... but I don't know off the top.
Apr 27 2007
prev sibling next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Walter,

 I keep racking my brain trying to come up with a short, pithy example
 of CTFE that uses AAs or struct literals. It should be just a few
 lines, and make it clear why CTFE is a great thing. I want to use it
 in presentations.
 
 Any ideas?
 
this is a union of a few things 1] my crazy idea to do a compieler in D CTFE 2] Jeff Nowakowski's post in the main D ng about the Unreal Engine How about a compile time "script to byte code" package? Some sort of VM that is totally integrated into your app could use byte code programs that are compiled by your engine at compile time.
Apr 27 2007
parent Alexander Panek <alexander.panek brainsware.org> writes:
On Sat, 28 Apr 2007 00:17:09 +0000 (UTC)
BCS <ao pathlink.com> wrote:
 [...]
 How about a compile time "script to byte code" package? Some sort of
 VM that is totally integrated into your app could use byte code
 programs that are compiled by your engine at compile time.
I like that idea. <3
Apr 28 2007
prev sibling next sibling parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
Walter Bright wrote:
 I keep racking my brain trying to come up with a short, pithy example of 
 CTFE that uses AAs or struct literals. It should be just a few lines, 
 and make it clear why CTFE is a great thing. I want to use it in 
 presentations.
 
 Any ideas?
a function that takes a class name and a few members, and generates method for saving and loading the object from a SQL databse, or something like that.
Apr 27 2007
prev sibling next sibling parent reply Robert Fraser <fraserofthenight gmail.com> writes:
Walter Bright Wrote:

 I keep racking my brain trying to come up with a short, pithy example of 
 CTFE that uses AAs or struct literals. It should be just a few lines, 
 and make it clear why CTFE is a great thing. I want to use it in 
 presentations.
 
 Any ideas?
How about one that, given a function pointer/delegate and the name of a Java package, wraps it for the JNI with the correct naming scheme and C calling convention.
Apr 28 2007
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Robert Fraser wrote:
 Walter Bright Wrote:
 
 I keep racking my brain trying to come up with a short, pithy example of 
 CTFE that uses AAs or struct literals. It should be just a few lines, 
 and make it clear why CTFE is a great thing. I want to use it in 
 presentations.

 Any ideas?
How about one that, given a function pointer/delegate and the name of a Java package, wraps it for the JNI with the correct naming scheme and C calling convention.
I don't think you could do that with CTFE. Unless I'm missing something, CTFE can only generate values, not code or types. Well, not unless you involve the mixin keyword, but there doesn't seem to be anything you can do with that that couldn't be done with templates. I think that, at this stage, CTFE doesn't really *have* one awesome example you can point to and say "is it not nifty?" The "problem" is that CTFE seems to be a nicer way of generating source code strings and other values at compile time. Maybe you could show CTFE being used for pre-computing a table of sine and cosine values, or show it being used to generate a source string as compared to an equivalent template version. That, or you could use that brainf*ck compiler someone wrote using CTFE. At least, I think it was CTFE... -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Apr 28 2007
prev sibling next sibling parent Benji Smith <dlanguage benjismith.net> writes:
Walter Bright wrote:
 I keep racking my brain trying to come up with a short, pithy example of 
 CTFE that uses AAs or struct literals. It should be just a few lines, 
 and make it clear why CTFE is a great thing. I want to use it in 
 presentations.
 
 Any ideas?
I think compile-time syntax checking of SQL strings, for a particular database engine, would be incredibly useful. Or maybe compile-time regex syntax checking. I don't know whether those examples would use AAs or struct literals, but they'd be damn useful. --benji
Apr 29 2007
prev sibling next sibling parent Jascha Wetzel <"[firstname]" mainia.de> writes:
it's more than a few lines, but the regex compiler i posted about
recently could be run at compile time with structs+AAs.

Walter Bright wrote:
 I keep racking my brain trying to come up with a short, pithy example of
 CTFE that uses AAs or struct literals. It should be just a few lines,
 and make it clear why CTFE is a great thing. I want to use it in
 presentations.
 
 Any ideas?
Apr 29 2007
prev sibling next sibling parent "David B. Held" <dheld codelogicconsulting.com> writes:
Walter Bright wrote:
 I keep racking my brain trying to come up with a short, pithy example of 
 CTFE that uses AAs or struct literals. It should be just a few lines, 
 and make it clear why CTFE is a great thing. I want to use it in 
 presentations.
 
 Any ideas?
struct CartesianPoint { double x; double y; }; struct PolarPoint { double r; double theta; }; CartesianPoint convert(PolarPoint pp) { return CartesianPoint(pp.r * cos(pp.theta), pp.r * sin(pp.theta)); } const CartesianPoint p = convert({ 2, 0.75 * pi }); I don't know how struct literals work, or if this is even a good example, but maybe something along these lines would be useful? Dave
Apr 29 2007
prev sibling parent eao197 <eao197 intervale.ru> writes:
On Sat, 28 Apr 2007 00:21:44 +0400, Walter Bright  =

<newshound1 digitalmars.com> wrote:

 I keep racking my brain trying to come up with a short, pithy example =
of =
 CTFE that uses AAs or struct literals. It should be just a few lines, =
=
 and make it clear why CTFE is a great thing. I want to use it in  =
 presentations.

 Any ideas?
Can I try? ;) Some languages have special syntax sugar for declaring attributes and = getters/setters methods for them. For example, in Scala we can write: class Demo( a: int, b: String, c: List[int] ) {} and class Demo would have attributes a, b, c, and getters/setters for = them. Or in Ruby: class Demo attr_accessor :a, :b, :c end But if I want to define attribute and getter/setter property for it I ne= ed = to write a lot of code (in comparision with Ruby or Scala). So I offer a= = simple CTFE function for attributes and propeties generation (see attach= ). For example: class Demo { mixin( implementAttrs( [ Attribute( "a", "int" ), Attribute( "b", "char[]" ), Attribute( "c", "int[]" ) ] ) ); } instead of: class Demo { int a_; public int a() { return a_; } public void a( int v ) { a_ =3D v; } ... } Disclaimer: it is not a production ready solution -- just an example :) -- = Regards, Yauheni Akhotnikau
May 03 2007