Idea : Expression Type
Xinok
xnknet at gmail.com
Thu Feb 1 09:54:36 PST 2007
Johan Granberg Wrote:
> votes++
>
> This is a good idea and could really complement mixins in a nice way.
> What should the limits of this syntax be? should this be allowed.
>
> //declared as
> expression loop(a,b) = foreach(i,s,a)b[i]=s;
>
> //used as
> char[] foo=new char[10];
> char[] bar=new char[10];
> loop(foo,bar);
>
> to be able to do this would be intresting as it could allow for some
> powerfull syntax abstractions.
>
> Another intresting case would be.
>
> //declared as this
> expression mix = AMixin!(bool)(5);
>
> //used like this
> mix;
>
> here mix would be a mixin doing some work rather than declaring variables
> and I would expect that the expression have there own scope so that no
> mixin variables is leaked into the surrounding namespace.
>
I think loops would be best left to functions. Just my opinion anyways, I think expressions should have a return value. If it contains a loop, then it has no return value and is nothing more than a command, aka function.
void loop(Ta, Tb)(Ta[] a, Tb[] b){ foreach(i,s; a)b[i]=s; } // In the case of dynamic arrays, it will point to the same block of data, so no pointer is necessary.
> 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.
> > 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;
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
I think expressions should be like structs though; Even though the expression is exactly the same, it should be treated as a different type.
expression mul1(a, b) = a * b;
expression mul2(a, b) = a * b;
is(mul1 == mul2) // False
temp!(mul1) is different than temp!(mul2)
More information about the Digitalmars-d
mailing list