Almighty Alias

alias OldType = NewType;
deprecated("use NewType instead") alias OldType = NewType;
alias Sqr = x => x * x;writeln([2, 3, 4].map!Sqr);
auto sqr(alias a)()
{
enum sqrA = a * a;
return sqrA;
}
enum x = sqr!10;
writeln(x);
alias Inc(alias a) = { a++; };int x = 1;
Inc!x();
writeln(x);
enum bool isClass(alias T) = is(T == class);class MyClass
{
int x;
}
assert(isClass!MyClass);
template ArrayOfSquares(T, T initValue)
{
T[10] array = initValue * initValue;
alias ArrayOfSquares = array;
}
auto a = ArrayOfSquares!(int, 2);
struct ArrayOfCubes(T, T initValue)
{
T[10] array = initValue * initValue * initValue;
}
auto cubes = ArrayOfCubes!(int, 2)();
// Error
void someFunc(T, T initValue)(ArrayOfCubes!(T, initValue) cubes)
{
writeln(initValue);
}
void someFunc(T, alias initValue)(ArrayOfCubes!(T, initValue) cubes)
{
writeln(initValue);
}
struct Foo
{
int x;
alias x this;
}
auto f = Foo(5);
writeln(f * 2); // same as f.x * 2
int x = 8;
alias a = x + 1; // Error

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store