example for compile time function execution

eao197 eao197 at intervale.ru
Thu May 3 05:13:47 PDT 2007


On Sat, 28 Apr 2007 00:21:44 +0400, Walter Bright  
<newshound1 at 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: implement_attrs.d
Type: application/octet-stream
Size: 1216 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20070503/277e273d/attachment.obj>


More information about the Digitalmars-d mailing list