Alias Arguments & Expressions

Xinok xnknet at gmail.com
Sun Jan 14 20:12:00 PST 2007


Frits van Bommel Wrote:

> Xinok wrote:
> > To accomplish what alias expressions could do, you would have to write all of this:
> > template mul(V...){
> > 	const typeof(V[0] * V[1]) mul = V[0] * V[1];
> > }
> > 
> > Compared to:
> > alias (a*b) mul(a, b);
> 
> That typeof(...) is redundant. Remove that and it should default to 
> automatic type deduction, which would do exactly the same.

Regardless, I think the extension to aliases would be a nice addition to D.

template mul(V...){ const auto mul = V[0] * V[1]; }
alias (a*b) mul(a, b);

One of the problems with the template implementation is it uses a tuple. First off of course is the V[] syntax. Sure you could define aliases or constants for the arguments, but this only further pushes the problem:

template mul(V...){
	// This currently doesn't work due to bugs in the compiler:
	// alias V[0] a; alias V[1] b;
	
	// D doesn't allow this with the 'auto' type:
	// const auto a = V[0], b = V[1];
	
	// This is the best you can do:
	const auto a = V[0]; const auto b = V[1];
	
	// So you can create symbols for the tuple arguments:
	const auto mul = a * b;
}

void main(){
	// Because 'a' and 'b' are members of the template, the compiler can't use the template implicitly:
	// writefln(mul!(15, 30));
	// Defining 'a' and 'b' as private doesn't work either
	
	// So you have to manually specify the member 'mul':
	writefln(mul!(15, 30).mul);
}


The second problem is tuples aren't self-documenting. The user sees (V...), they'll have no idea what arguments they're supposed to pass. Alias arguments on the other hand can be named, giving the user a small idea what arguments must be provided for the expression.



More information about the Digitalmars-d mailing list