Alias Arguments & Expressions

Chris Nicholson-Sauls ibisbasenji at gmail.com
Mon Jan 15 21:16:44 PST 2007



Xinok wrote:
> 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.

First, I really don't think its a completely bad idea, although I might prefer the 
template parameters being on the alias keyword, even if it is a change from how its done 
with classes, structs, etc.  Something like:
alias(a, b) (a * b) mul;

Second, I don't really comprehend what you are trying to do with the template above... it 
would just be:
# template mul (a, b) { const mul = a * b; }
#
# void main () {
#   writefln(mul!(15, 30));
# }

Which, IMHO, isn't so bad.  (Note that 'auto' in this case isn't needed for type 
inferance, as any storage class without a specified type defaults to auto.)

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list