if-expressions

sambeau no_spam_sambeau at mac.com
Mon May 28 06:30:05 PDT 2007


Bent Rasmussen Wrote:

 I remembered a feature of 
> another language (haXe) I've been using for some light-weight work. It has 
> the nice property that `if´ is an expression. So I was wondering if this 
> would be nice in D as well...

</lurk>

I have been writing/using a web scripting language for the past 5 years that makes great use of this feature. It (and the complementary 'for' construct) are really useful.

I suspect they could have real use in D's compile time templates.

As web scripting is essentially just fancy macros (text goes in - text with tags goes out) I wrote 'Basil' a simple template concatenator. It can do things like:

  xs = {1 2 3 4 5}
  <ul>
  result = for (x in xs) {
    <li>x</li>
  }
  </ul>

(returns: "<ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>
and

  pluralize = if(n ne 1) {{s}}

As everything is a string (eg {..}) and everything returns a string and all strings can be evaluated it makes for some interesting design patterns. In places it is almost Lisp-like.

I have had thoughts about trying it out as a macro language for other languages. It so far generates HTML and SQL neatly enough. Why not C/++/D?..

<lurk>



More information about the Digitalmars-d mailing list