Browse Source

chore: Adding OO features to ECAL documentation

Matthias Ladkau 3 years ago
parent
commit
08da30c5f8
1 changed files with 50 additions and 0 deletions
  1. 50 0
      lang/ecal/README.md

+ 50 - 0
lang/ecal/README.md

@@ -205,6 +205,56 @@ d := c["foo"]
 e := c.foo
 ```
 
+Object-oriented programming structures
+--------------------------------------
+ECAL supports Object-oriented programming by providing the concept of objects containing data as properties and code in the form of methods. Methods can access properties of their object by using the variable `this`. Objects can be initialized with a constructor. Objects can inherit data and properties from each other. Multiple inheritance is allowed. Constructors of super map structures can be called by using the `super` function list variable available to the constructor of an object.
+
+Operator|Description
+-|-|-
+new|In-build function to instantiate a map structure into an object
+super|Property with a list value containing all super map structures and constructor method variable which contains a list of all super map structure constructors
+init|Attribute with a constructor function as value - this function can use the variable `super` to access constructors of super map structures
+this|Method variable containing the instantiated object
+
+Example:
+```
+Bar := {
+  ...
+}
+
+Foo := {
+  "super" : [ Bar ]
+
+  # Object IDs
+  #
+  "id" : 0
+  "idx" : 0
+
+  # Constructor
+  #
+  "init" : func(id) {
+    super[0]()
+    this.id := id
+  }
+
+  # Return the object ID
+  #
+  "getId" : func() {
+      return this.idx
+  }
+
+  # Set the object ID
+  #
+  "setId" : func(id) {
+      this.idx := id
+  }
+}
+
+FooObject := new(Foo, 123)
+FooObject.setId(500)
+result := FooObject.getId() + FooObject.id # 623
+```
+
 Loop statements
 ---------------
 All loops are defined as a 'for' block statement. Counting loops are defined with the 'range' function. The following code iterates from 2 until 10 in steps of 2: