Update page 'P# 1.0 Draft Specification'

2018-07-17 12:45:53 +02:00
parent f68df12b31
commit fbacef7ad4

@@ -499,24 +499,77 @@ Both constructor and destructor methods cannot return any value. Declaring a con
%>
### 8.7. Constructor Overloading
P# supports constructor overloading like any other method.
### 8.8. Inheritance
### 8.8. Scope Resolution Operator
In computer programming, scope is an enclosing context where values and expressions are associated. The scope resolution operator helps to identify and specify the context to which an identifier refers, particularly by specifying a namespace. The specific uses vary across different programming languages with the notions of scoping. In P# language the scope resolution operator is written "::".
### 8.9. Virtual Classes & Methods
### 8.9. Inheritance
The inheritance is a way to form new classes using classes that have already been defined. The newly formed classes are called derived classes, the classes that we derive from are called base classes. Important benefits of inheritance are code reuse and reduction of complexity of a program. The derived classes (descendants) override or extend the functionality of base classes (ancestors).
### 8.10. Magic Methods
<%
class Base {
void __construct() {
print('World!');
}
}
### 8.11. Class Constants
class Derived extends Base {
void __construct() {
print('Hello ');
parent::__construct();
}
}
%>
### 8.12. Static Keyword
In this script, there are two classes: a Base class and a Derived class. The Derived class inherits from the Base class. In P#, the extends keyword is used to create inheritance relations. In the constructor of the Derived class, we call the parent constructor. We use the parent keyword, followed by two colons and a method name. The constructors of the parent classes must be called explicitly. Output od the above example is "Hello World!'.
### 8.13. Final Keyword
### 8.10. Virtual Classes & Methods
Virtual classes cannot be instantiated. If a class contains at least one virtual method, it must be declared virtual too. Virtual methods cannot be implemented, they merely declare the methods' signatures. When a class inherits from a virtual class, all virtual methods must be implemented by the derived class. Furthermore, these methods must be declared with the same or with a less restricted visibility.
Unlike interfaces, virtual classes may have methods with full implementation and may also have defined member fields. So virtual classes may provide a partial implementation.
### 8.14. Instanceof Keyword
<%
virtual class Drawing {
protected int $x = 0;
protected int $y = 0;
### 8.15. Interfaces
public abstract float area();
### 8.16. Exceptions
public void getCoordinates() {
print("\$x is $this->x\n");
print("\$y is $this->y\n");
}
}
class Circle extends Drawing {
private int $radius;
public void __construct(int $x, int $y, int $r) {
$this->radius = $r;
$this->x = $x;
$this->y = $y;
}
public float area() {
return $this->radius * $this->radius * 3.14;
}
}
%>
### 8.11. Magic Methods
When the print keyword is used with the object instance, the \__toString() magic method is called.
### 8.12. Class Constants
### 8.13. Static Keyword
### 8.14. Final Keyword
### 8.15. Instanceof Keyword
### 8.16. Interfaces
### 8.17. Exceptions
## 9. Anonymous Functions
Anonymous functions, also known as closures, allow the creation of functions which have no specified name. In P#, anonymous function arguments can take a default value exactly like standard function arguments. The default value associated with the function argument can be any complex expression including function calls.