- D - Conditional Compilation
- D Programming - Contract
- D Programming - Exception Handling
- D Programming - Concurrency
- D Programming - File I/O
- D Programming - Immutables
- D Programming - Templates
- D Programming - Modules
- D Programming - Mixins
- D Programming - Aliases
- D Programming - Ranges
- D Programming - Unions
- D Programming - Structs
- D Programming - Tuples
- D Programming - Pointers
- D Programming - Associative Arrays
- D Programming - Arrays
- D Programming - Strings
- D Programming - Characters
- D Programming - Functions
- D Programming - Decisions
- D Programming - Loops
- D Programming - Operators
- D Programming - Literals
- D Programming - Enums
- D Programming - Data Types
- D Programming - Variables
- D Programming - Basic Syntax
- D Programming - Environment
- D Programming - Overview
- D Programming - Home
D Programming - Object Oriented
- D Programming - Abstract Classes
- D Programming - Interfaces
- D Programming - Encapsulation
- D Programming - Overloading
- D Programming - Inheritance
- D Programming - Classes & Objects
D Programming - Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
D Programming - Apases
Apas, as the name refers provides an alternate name for existing names. The syntax for apas is shown below.
apas 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.
apas 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 apas name instead of the expression.
apas expression apas_name ;
As you may know, a typedef adds the abipty to create new types. Apas can do the work of a typedef and even more. A simple example for using apas is shown below that uses the std.conv header which provides the type conversion abipty.
import std.stdio; import std.conv:to; apas 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 apas name toString making it more convenient and simpler to understand.
Apas for a Tuple
Let us a look at another example where we can set apas name for a Tuple.
import std.stdio; import std.typetuple; apas TypeTuple!(int, long) TL; void method1(TL tl) { writeln(tl[0]," ", 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 apas variable and it simppfies the method definition and access of variables. This kind of access is even more useful when we try to reuse such type tuples.
Apas for Data Types
Many times, we may define common data types that needs to be used across the apppcation. When multiple programmers code an apppcation, it can be cases where one person uses int, another double, and so on. To avoid such confpcts, we often use types for data types. A simple example is shown below.
Example
import std.stdio; apas int myAppNumber; apas 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
Apas 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 apas, 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
and 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"; } apas 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
Apas This
Apas this provides the capabipty of automatic type conversions of user-defined types. The syntax is shown below where the keywords apas and this are written on either sides of the member variable or member function.
apas member_variable_or_member_function this;
Example
An example is shown below to show the power of apas this.
import std.stdio; struct Rectangle { long length; long breadth; double value() const @property { return cast(double) length * breadth; } apas 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 apas this method.
When the above code is compiled and executed, it produces the following result −
30Advertisements