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

--

--

--

Computer graphics developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Setting up the connection between AWS RDS PostgreSQL database and python

Everyone Learn About CCNP Training

Code edit directly through ssh of remote server using locally installed fav text editor

competitive Programming

Things I wish I could have done in Engineering

Story of my life

Adding Class Scripts to your Chatbot in Teneo Studio

Git In Action: Part 2

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
Timur Gafarov

Timur Gafarov

Computer graphics developer

More from Medium

What Is Architectural Runway In SAFe®?

Gates, 2 dollars and Chicken

What Is Non-Sleep Deep Rest? (And why does Google CEO Sundar Pichai use it to unwind?)

There is no post-crisis for food — we still need to feed the planet