Jun 10, 2023Kuina-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.
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.
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.
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).
- func main()
- class Parent()
- +func f(n: int): int
- ret n * n
- end func
- end class
-
- class Child(Parent)
- +*func f(n: int): int
- ret super(me, n) + n
- end func
- end class
-
- var child: Child :: #Child
- do cui@print(child.f(3).toStr() ~ "\n")
- end func
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.
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.
- func main()
- class Test()
- +var a: int
- *func ctor()
- do me.a :: 5
- end func
- end class
-
- var test: Test :: #Test
- do cui@print(test.a.toStr() ~ "\n")
- end func
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.
+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.
- func main()
- class Test()
- +var a: int
- +var b: int
- +*func cmp(t: kuin@Class): int
- var t2: Test :: t $ Test
- if(me.a + me.b > t2.a + t2.b)
- ret 1
- elif(me.a + me.b < t2.a + t2.b)
- ret - 1
- else
- ret 0
- end if
- end func
- end class
-
- var test1: Test :: #Test
- do test1.a :: 2
- do test1.b :: 3
- var test2: Test :: #Test
- do test2.a :: 4
- do test2.b :: 5
-
- do cui@print((test1 >= test2).toStr() ~ "\n")
- end func
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).
- func main()
- class Parent()
- end class
-
- class Child(Parent)
- end class
-
- var parent: Parent :: #Child
- end func
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).
- func main()
- class Parent()
- end class
-
- class Child(Parent)
- end class
-
- var parent: Parent
-
- do parent :: #Child
- var child1: Child :: parent $ Child {Cast Success}
-
- do parent :: #Parent
- var child2: Child :: parent $ Child {Cast Failure}
- end func
You can use the "=$" or "<>$" operator to check if the cast will succeed or not.