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 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
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 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 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:
------------QKVMIMmI4GKolQhlqy5w8U
Content-Type: text/plain; format=flowed; delsp=yes; charset=koi8-r
Content-Transfer-Encoding: Quoted-Printable

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 =

 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 ------------QKVMIMmI4GKolQhlqy5w8U Content-Disposition: attachment; filename=implement_attrs.d Content-Type: application/octet-stream; name=implement_attrs.d Content-Transfer-Encoding: Base64 aW1wb3J0IHRhbmdvLmlvLlN0ZG91dDsNCg0Kc3RydWN0IEF0dHJpYnV0ZQ0KICB7 DQogICAgY2hhcltdIG5hbWVfOw0KICAgIGNoYXJbXSB0eXBlXzsNCiAgfQ0KDQpj aGFyW10NCmltcGxlbWVudEF0dHJzKCBBdHRyaWJ1dGVbXSBhdHRyaWJ1dGVzICkN CiAgew0KICAgIGNoYXJbXSByZXN1bHQ7DQogICAgZm9yZWFjaCggYTsgYXR0cmli dXRlcyApDQogICAgICB7DQogICAgICAgIHJlc3VsdCB+PSBhLnR5cGVfIH4gIiAi IH4gYS5uYW1lXyB+ICJfO1xuIiB+DQogICAgICAgICAgICAicHVibGljICIgfiBh LnR5cGVfIH4gIiAiIH4gYS5uYW1lXyB+ICIoKSB7IHJldHVybiAiIH4NCiAgICAg ICAgICAgICAgICBhLm5hbWVfIH4gIl87IH1cbiIgfg0KICAgICAgICAgICAgInB1 YmxpYyB2b2lkICIgfiBhLm5hbWVfIH4gIiggIiB+IGEudHlwZV8gfiAiIHYgKSB7 ICIgfg0KICAgICAgICAgICAgICAgIGEubmFtZV8gfiAiXyA9IHY7IH1cbiI7DQog ICAgICB9DQoNCiAgICByZXR1cm4gcmVzdWx0Ow0KICB9DQoNCmNsYXNzIE1lZXRp bmdQbGFjZQ0KICB7DQogICAgdHlwZWRlZiBpbnQgQ29sb3I7DQoNCiAgICBtaXhp biggaW1wbGVtZW50QXR0cnMoDQogICAgICAgIFsgQXR0cmlidXRlKCAicmVtYWlu aW5nIiwgInVpbnQiICksDQogICAgICAgICAgQXR0cmlidXRlKCAiY3JlYXR1cmVz SW5QbGFjZSIsICJ1aW50IiApLA0KICAgICAgICAgIEF0dHJpYnV0ZSggImNvbG9y IiwgIkNvbG9yIiApIF0gKSApOw0KDQogICAgdGhpcyggdWludCByZW1haW5pbmcg KQ0KICAgICAgew0KICAgICAgICByZW1haW5pbmdfID0gcmVtYWluaW5nOw0KICAg ICAgICBjcmVhdHVyZXNJblBsYWNlXyA9IDA7DQogICAgICAgIGNvbG9yXyA9IDA7 DQogICAgICB9DQogIH0NCg0Kdm9pZA0KbWFpbigpDQogIHsNCiAgICBhdXRvIG1l ZXRpbmdQbGFjZSA9IG5ldyBNZWV0aW5nUGxhY2UoIDEwMCApOw0KICAgIG1lZXRp bmdQbGFjZS5jcmVhdHVyZXNJblBsYWNlID0gMzA7DQogICAgbWVldGluZ1BsYWNl LmNvbG9yID0gMzsNCg0KICAgIGFzc2VydCggbWVldGluZ1BsYWNlLnJlbWFpbmlu ZyA9PSAxMDAgKTsNCiAgICBhc3NlcnQoIG1lZXRpbmdQbGFjZS5jcmVhdHVyZXNJ blBsYWNlID09IDMwICk7DQogICAgYXNzZXJ0KCBtZWV0aW5nUGxhY2UuY29sb3Ig PT0gMyApOw0KICB9DQoNCi8vIHZpbTp0cz0yOnN0cz0yOnN3PTI6ZXhwYW5kdGFi OmZlbmM9dXRmLTg6DQoNCg== ------------QKVMIMmI4GKolQhlqy5w8U--
May 03 2007