Wednesday, December 07, 2005

Built in Code Generation

I'm always looking for new ideas in programming languages. While looking at the Ferite language, I came across something I had never seen before; something the Ferite folks call a 'directive'. A directive lets you instruct the compiler to execute certain parts of the code at compile time in order to modify the existing code. It's like COG but built into the language. I don't know if the people behind Ferite came up with the idea themselves or if they borrowed this from another language, but regardless, It's the first time I've seen it. An example follows:




class modifies Obj {

directive access( ... ) {
array a = arguments();
string code = "class modifies ${Class.name(self)} {";
Array.each( a ) using ( name ) {
if( name isa string ) {
code += "
function set$name( void value ) {
.$name = value;
}
function get$name() {
return .$name;
}";
}
};
code += "}";
eval( code );
}
}

class Example {
private number X = 10;
private string foo = "hi";

[access X, foo];
}

Post a Comment
 
The Out Campaign: Scarlet Letter of Atheism