Aug 13, 2022Kuina-chan
This is the language specification 7 of Kuin Programming Language, and is about classes.
In Kuin, a class is defined as shown in Table 1-1.
class Name(Class Name Of Inherited Source)
If you write the class name in the parenthesis, it will be the class that inherits from that class. If you omit it, the class will inherit from kuin@Class. Therefore, all classes will eventually inherit from kuin@Class when traced back to the source of inheritance.
For member definitions, alias statements, const statements, var statements, class blocks, enum blocks, and func blocks can be written.
Of these, the var statement is a class property (member variable), and the func block is a method (member function). The rest of these definitions can only be referenced within the class.
The class specification is shown in Figure 1-1.
Methods can be overridden by inheriting methods of the same name in the source class by prefixing the definition with "*".
The class specification is shown in Figure 2-1.
To call an inherited source method from within a destination method, use "super". super is a function that can only be referenced within the destination method, and is called by passing me as the first argument (Figure 2-2).
When this program is executed, the f method of the Parent class will be called to calculate "3*3=9", and then "9+3=12" will be calculated in the f method of the Child class, and "12" will be displayed.
Among the methods defined in kuin@Class, ctor and cmp are inheritable.
When you inherit from the ctor method, you can implement the constructor (the initialization process at the time of instance creation). The definition of the ctor method is shown in Table 3-1.
If you do not inherit from the ctor method, it will be an empty method that does not do anything. An example implementation of the ctor method is shown in Figure 3-1.
When this program is executed, the ctor method of the Test class will be called at the "#Test" point, and eventually "5" will be displayed on the screen.
By inheriting from the cmp method, the class comparison process can be implemented. The definition of the cmp method is shown in Table 3-2.
+func cmp(t: kuin@Class): int
|t||Value to be compared.|
|Return Value||Returns positive if itself is greater than the value to be compared, negative if itself is less than the value to be compared, and 0 if itself is equal to the value to be compared.|
If the cmp method is not inherited, an exception (0xE9170004) will be raised during comparison, and the class will not be able to be compared. An example of using the cmp method is shown in Figure 3-2.
In this example, the comparison method of the Test class is defined as the sum of a and b.
An instance of a class can only be converted to the types of its own class and its inherited classes (Figure 4-1).
However, when converting to an inherited class, you must explicitly cast it using the "$" operator or you will get a compilation error. In this case, if the instance is converted to the type of the class of the instance and the inherited class, it can be converted, but if it is converted to the inheriting class, an exception (0xE9170001) will be raised (Figure 4-2).
You can use the "=$" or "<>$" operator to check if the cast will succeed or not.