Aug 13, 2022Kuina-chan


This is the language specification 7 of Kuin Programming Language, and is about classes.

1Kuin Class

In Kuin, a class is defined as shown in Table 1-1.
Table 1-1: Class Definition

class Name()
    Member Definitions
end class

class Name(Class Name Of Inherited Source)
    Member Definitions
end class

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.
  • When a class is inherited, all members of the inherited class are inherited. If the inherited class is also inherited, all the members are inherited by following the inherited source.
  • Among the members, only properties and methods become public members by prefixing the definition with "+" and are exposed to the outside of the class. Only public members can be referenced from outside the class.
  • In the method, an instance of itself can be referred to by the variable me.
Figure 1-1: Class Specification

2Inheritance

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.
  • Destination methods must match the inherited methods in terms of the presence or absence of a "+", the type and name of arguments, and the definition of the return value.
  • When both "+" and "*" are specified, they should be written in the order "+*".
  • A compile error will occur if you inherit a method that does not exist in the inherited source class, or if you define a method with the same name as a method that exists in the inherited source class without inheritance.
Figure 2-1: Inheritance Specification
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).
  1. func main()
  2.   class Parent()
  3.     +func f(n: int): int
  4.       ret n * n
  5.     end func
  6.   end class
  7.  
  8.   class Child(Parent)
  9.     +*func f(n: int): int
  10.       ret super(me, n) + n
  11.     end func
  12.   end class
  13.  
  14.   var child: Child :: #Child
  15.   do cui@print(child.f(3).toStr() ~ "\n")
  16. end func
Figure 2-2: Example Of Using super Function
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.

3ctor And cmp

Among the methods defined in kuin@Class, ctor and cmp are inheritable.

3.1ctor



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.
Table 3-1: Definition Of kuin@Class.ctor

func ctor()

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.
  1. func main()
  2.   class Test()
  3.     +var a: int
  4.     *func ctor()
  5.       do me.a :: 5
  6.     end func
  7.   end class
  8.  
  9.   var test: Test :: #Test
  10.   do cui@print(test.a.toStr() ~ "\n")
  11. end func
Figure 3-1: Example Of ctor Method Implementation
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.

3.2cmp



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.
Table 3-2: Definition Of kuin@Class.cmp

+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.
  1. func main()
  2.   class Test()
  3.     +var a: int
  4.     +var b: int
  5.     +*func cmp(t: kuin@Class): int
  6.       var t2: Test :: t $ Test
  7.       if(me.a + me.b > t2.a + t2.b)
  8.         ret 1
  9.       elif(me.a + me.b < t2.a + t2.b)
  10.         ret - 1
  11.       else
  12.         ret 0
  13.       end if
  14.     end func
  15.   end class
  16.  
  17.   var test1: Test :: #Test
  18.   do test1.a :: 2
  19.   do test1.b :: 3
  20.   var test2: Test :: #Test
  21.   do test2.a :: 4
  22.   do test2.b :: 5
  23.  
  24.   do cui@print((test1 >= test2).toStr() ~ "\n")
  25. end func
Figure 3-2: Example Of cmp Method Implementation
In this example, the comparison method of the Test class is defined as the sum of a and b.

4Casting A Class

An instance of a class can only be converted to the types of its own class and its inherited classes (Figure 4-1).
  1. func main()
  2.   class Parent()
  3.   end class
  4.  
  5.   class Child(Parent)
  6.   end class
  7.  
  8.   var parent: Parent :: #Child
  9. end func
Figure 4-1: Assigning To Inherited Source Class
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).
  1. func main()
  2.   class Parent()
  3.   end class
  4.  
  5.   class Child(Parent)
  6.   end class
  7.  
  8.   var parent: Parent
  9.  
  10.   do parent :: #Child
  11.   var child1: Child :: parent $ Child {Cast Success}
  12.  
  13.   do parent :: #Parent
  14.   var child2: Child :: parent $ Child {Cast Failure}
  15. end func
Figure 4-2: Assigning To Inheriting Class
You can use the "=$" or "<>$" operator to check if the cast will succeed or not.
1660379676enf