Proposal: __traits(code, ...) and/or .codeof

Timon Gehr timon.gehr at gmx.ch
Fri Mar 23 02:37:16 PDT 2012


On 03/23/2012 12:33 AM, F i L wrote:
> Timon Gehr wrote:
>> We have the macro keyword. I envision something like:
>>
>> macro replaceAggregate(macro newAggregate, macro loop : foreach(x;
>> aggr){statements}, macro x, macro aggr, macro statements) {
>> foreach(x; newAggregate){statements}
>> }
>>
>> void main(){
>> int[] a = [1,2,3];
>> int[] b = [2,3,4];
>> replaceAggregate(b, foreach(x;a){writeln(x);});
>> }
>>
>> (The syntax looks horrible, but you get the idea: AST walking by
>> pattern matching)
>
> This looks substantially more complicated than what I had in mind. I
> think it's a great idea, but something that could be added after the
> initial functionality was there.
>
>
>> macro until(macro condition, string str){
>> while(!condition){
>> mixin(str); // string and mixin not strictly necessary,
>> // maybe enable the feature on macro params too
>> }
>> }
>>
>> void main(){
>> bool done = false;
>> int x;
>> until(done){
>> done = foo(x++);
>> }
>> }
>>
>> This is just a very rough sketch though, we would need a much more
>> refined design. I think getting the symbol scoping right is most
>> important.
>
> I'm a bit confused about what's actually going on here, but it certainly
> looks interesting. What exactly is being passed to "string str" in the
> macro?

The idea is that if you have something of the form:

identifier(arguments) {
     body
}

It would get transformed into:

identifier(arguments, q{
     body
});







More information about the Digitalmars-d mailing list