Is metaprogramming useful?

Andrey Khropov andkhropov_nosp at m_mtu-net.ru
Wed Nov 29 16:07:02 PST 2006


Georg Wrede wrote:

> It's not metaprogramming itself, it's the bad implementations that make it
> hard.
> 
> If we had Perfect Metaprogramming(TM) in D, then I could do the following:
> 
>   I'm coding some stuff and I notice that what I'd really want
>   is a new keyword, "unless", that would make it so much easier
>   for me to write this application clearly. I decide to create it.
> 
>   I want to use it like this
> 
>     unless (fullMoonTonight) { doRegularStuff() }
> 
>   So, to create such a thing in D, I'd write something like
> 
>     define("unless", "(", BooleanExpression, ")", BlockStatement)
>     {
>       if(!BooleanExpression) BlockSTatement;
>     }
> 
> Now, if I'd made errors in writing the meta code, then the compiler would
> error me, of course. No biggie. And since the D compiler would understand
> what's going on (as opposed to the C preprosessor or compiler), the error
> messages would be what we're used to in D.
> 
> Later, when I actually use the "unless" construct, again the error messages
> would be normal because D now understands what "unless" is all about.

Well, I'm sorry I'm saying that again but it's almost exactly the way Nemerle
does it.

And it actually has that unless macro :-) 
Here's the actual code from the compiler svn:

-------------------------------------------------------------------
macro @unless (cond, body)
syntax ("unless", "(", cond, ")", body) 
{
    <[ match ($cond) { | false => $body : void | _ => () } ]>
}
-------------------------------------------------------------------

but it's defined using pattern matching.

anyway it can be defined your way:

-------------------------------------------------------------------
macro @unless (cond, body)
syntax ("unless", "(", cond, ")", body) 
{
    <[ when( !($cond) ) $body ]>
}
-------------------------------------------------------------------

('when' is used in Nemerle for 'if without else') 

> ---
> 
> People say GC has to be slow, but that is mostly because it used to only
> exist in languages that were slow to begin with (and badly implemented).

Sad to say, but GC in D is still conservative and hence slow :-(

-- 
AKhropov



More information about the Digitalmars-d mailing list