Idea : Expression Type

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Thu Feb 1 10:02:30 PST 2007


Xinok wrote:
>> Walter had the idea of allowing an expression to bind to an alias,
>> and to allow the following:
>> 
>> template mul(alias a, alias b) { alias (a * b) mul; }
> 
> The problem with aliases is that the end expression must be constant.
> Expressions are more like functions and can give non-constant values.

Nonono. As I said (in the paragraph you quoted above), expressions could 
bind to aliases, so mul accepts any two expressions and creates an 
expression.

>>> One example of a recent post, the max function: expression max(a,
>>> b) = a > b ? a : b; Can handle any and all types, and you can use
>>> it as an l-value.
>> Nah, that won't work. It will doubly evaluate one of its arguments.
>> But the feature will allow very simple implementation of my
>> varargs_reduce and varargs_reduce_parallel primitives, e.g.:
> 
> This is something I've thought about myself. One possible solution
> though is lazy arguments. If you think about it, the expression given
> to a lazy argument is only evaluated once, and it remembers the
> value. The same could be possible for expressions.
> 
> expression max(lazy a, lazy b) = a > b ? a : b;

But this semantics is opposite to lazy's current semantics.

Overall, I think the alias-based macros are more in keep with the 
current language and allow more expressive constructs beyond expression 
manipulation.

> One topic I forgot to mention is using expressions as template
> arguments. This is the idea I had for it:
> 
> template temp(expression a(a, b)){ } // Must have two arguments, a
> and b template temp(expression b){ } // Can accept any set of
> arguments
> 
> expression mul(a, b) = a * b;
> 
> temp!(mul); // Expression is given to template

Yes. This is exactly Walter's idea: allow an expression to bind to an 
alias. No need for a new keyword.


Andrei



More information about the Digitalmars-d mailing list