Alias Arguments & Expressions
Xinok
xnknet at gmail.com
Sun Jan 14 15:21:29 PST 2007
I think aliases should be expanded to allow for arguments, and allow constant expressions. The syntax that I used could be added to the language without breaking any existing code.
-- Arguments
Like with functions and classes, the arguments could simply add a template to the alias.
alias T* ptr(T);
would be the same as writing:
template ptr(T){ alias T* ptr; }
And they would be instantiated just like templates:
ptr!(int)
-- Expressions
alias 10 base;
alias (10*20) val;
Combine with arguments:
alias (a*b) mul(a, b);
I'm sure these ideas have been posted before, but I think they could really be useful. For example, I once wrote an encryption algorithm which would encrypt in 128k blocks (131072 bytes). I could have defined an alias to perform the basic math for this:
alias (n * 131072) SizeofBlocks(n);
SizeofBlocks!(8);
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);
The syntax for aliases would be simpler and quicker. Plus, you wouldn't have to guess the base type of the expression.
More information about the Digitalmars-d
mailing list