Aspect oriented programming: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring, p2js note) |
No edit summary |
||
Line 20: | Line 20: | ||
;Task |
;Task |
||
The task is to describe or show how, or to what extent, a given programming language implements, or is able to implement or simulate, Aspect Oriented Programming. |
The task is to describe or show how, or to what extent, a given programming language implements, or is able to implement or simulate, Aspect Oriented Programming. |
||
=={{header|Ada}}== |
|||
The primary unit of modularity in Ada is the package. Ada provides the ability to create child packages with visibility into portions of their parent package without modifying the parent package. The child package can provided extended capabilities based upon the data types, data elements and subprograms defined in the parent package. |
|||
Example parent package specification: |
|||
<lang Ada>package parent is |
|||
function Add2 (X : in Integer) return Integer; |
|||
end parent;</lang> |
|||
Example parent package body: |
|||
<lang Ada>package body parent is |
|||
function Add2 (X : in Integer) return Integer is |
|||
begin |
|||
return X + 2; |
|||
end Add2; |
|||
end parent;</lang> |
|||
Example child package specification: |
|||
<lang Ada>package parent.child is |
|||
function Add2 (X : in Integer) return Integer; |
|||
end parent.child;</lang> |
|||
Example child package specification: |
|||
<lang Ada>with Ada.Text_IO; use Ada.Text_IO; |
|||
package body parent.child is |
|||
function Add2 (X : in Integer) return Integer is |
|||
begin |
|||
Put_Line ("Added 2 to " & X'Image); |
|||
return parent.Add2 (X); |
|||
end Add2; |
|||
end parent.child;</lang> |
|||
The following program demonstrates calling both the Add2 function from the parent package and calling the Add2 function from the child package. The child package has visibility to the public portion of the parent package and can therefore call the parent's Add2 function directly. |
|||
<lang Ada>with parent.child; |
|||
with Ada.Text_IO; use Ada.Text_IO; |
|||
procedure Main is |
|||
Num : Integer := 5; |
|||
Result : Integer; |
|||
begin |
|||
Put_Line ("Calling parent Add2 function:"); |
|||
Result := parent.Add2 (Num); |
|||
Put_Line ("Result : " & Result'Image); |
|||
New_Line; |
|||
Put_Line ("Calling parent.child Add2 function"); |
|||
Result := parent.child.Add2 (Num); |
|||
Put_Line ("Result : " & Result'Image); |
|||
end Main;</lang> |
|||
{{output}} |
|||
<pre> |
|||
Calling parent Add2 function: |
|||
Result : 7 |
|||
Calling parent.child Add2 function |
|||
Added 2 to 5 |
|||
Result : 7 |
|||
</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |