D Programming - Aliases



Alias, as the name refers provides an alternate name for existing names. The syntax for alias is shown below.

alias new_name = existing_name;

The following is the older syntax, just in case you refer some older format examples. Its is strongly discouraged the use of this.

alias existing_name new_name; 

There is also another syntax that is used with expression and it is given below in which we can directly use the alias name instead of the expression.

alias expression alias_name ;

As you may know, a typedef adds the ability to create new types. Alias can do the work of a typedef and even more. A simple example for using alias is shown below that uses the std.conv header which provides the type conversion ability.

import std.stdio; 
import std.conv:to; 
 
alias to!(string) toString;  

void main() { 
   int a = 10;  
   string s = "Test"~toString(a); 
   writeln(s); 
}

When the above code is compiled and executed, it produces the following result −

Test10 

In the above example instead of using to!string(a), we assigned it to alias name toString making it more convenient and simpler to understand.

Alias for a Tuple

Let us a look at another example where we can set alias name for a Tuple.

import std.stdio; 
import std.typetuple; 
 
alias TypeTuple!(int, long) TL; 
 
void method1(TL tl) { 
   writeln(tl[0],"\t", tl[1] ); 
} 
 
void main() { 
   method1(5, 6L);    
}

When the above code is compiled and executed, it produces the following result −

5	6

In the above example, the type tuple is assigned to the alias variable and it simplifies the method definition and access of variables. This kind of access is even more useful when we try to reuse such type tuples.

Alias for Data Types

Many times, we may define common data types that needs to be used across the application. When multiple programmers code an application, it can be cases where one person uses int, another double, and so on. To avoid such conflicts, we often use types for data types. A simple example is shown below.

Example

import std.stdio;
  
alias int myAppNumber; 
alias string myAppString;  

void main() { 
   myAppNumber i = 10; 
   myAppString s = "TestString"; 
   
   writeln(i,s);   
}

When the above code is compiled and executed, it produces the following result −

10TestString

Alias for Class Variables

There is often a requirement where we need to access the member variables of the superclass in the subclass, this can made possible with alias, possibly under a different name.

In case you are new to the the concept of classes and inheritance, have a look at the tutorial on classes and inheritance before starting with this section.

Example

A simple example is shown below.

import std.stdio; 
 
class Shape { 
   int area; 
}
  
class Square : Shape { 
   string name() const @property { 
      return "Square"; 
   } 
   alias Shape.area squareArea; 
}
   
void main() { 
   auto square = new Square;  
   square.squareArea = 42;  
   writeln(square.name); 
   writeln(square.squareArea); 
}

When the above code is compiled and executed, it produces the following result −

Square 
42

Alias This

Alias this provides the capability of automatic type conversions of user-defined types. The syntax is shown below where the keywords alias and this are written on either sides of the member variable or member function.

alias member_variable_or_member_function this; 

Example

An example is shown below to show the power of alias this.

import std.stdio;
  
struct Rectangle { 
   long length; 
   long breadth;  
   
   double value() const @property { 
      return cast(double) length * breadth; 
   }
   alias value this; 
} 
double volume(double rectangle, double height) {
   return rectangle * height; 
}
  
void main() { 
   auto rectangle = Rectangle(2, 3);  
   writeln(volume(rectangle, 5)); 
}

In the above example, you can see that the struct rectangle is converted to double value with the help of alias this method.

When the above code is compiled and executed, it produces the following result −

30
Advertisements