Thread overview
example for compile time function execution
Apr 27, 2007
Walter Bright
Apr 27, 2007
Dan
Apr 28, 2007
BCS
Apr 28, 2007
Alexander Panek
Apr 28, 2007
Hasan Aljudy
Apr 29, 2007
Benji Smith
Apr 29, 2007
Jascha Wetzel
Apr 30, 2007
David B. Held
May 03, 2007
eao197
April 27, 2007
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?
April 27, 2007
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.
April 28, 2007
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.


April 28, 2007

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.
April 28, 2007
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
April 29, 2007
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
April 29, 2007
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?
April 30, 2007
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
May 03, 2007
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 need 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_ = v; }
     ...
   }

Disclaimer: it is not a production ready solution -- just an example :)

-- 
Regards,
Yauheni Akhotnikau