Classes is Type-C serve to define objects with hidden state and behavior. Classes are the building blocks of object-oriented programming, and they allow for the creation of complex data structures and systems. Classes in Type-C are defined using the class keyword, followed by the class name and a block of code that contains the class attributes and methods.
Basic Example:
Encapsluation
Type-C supports encapsulation through the local keyword, indicating that an attribute or method is only accessible within the class. Lack of local indicates that the attribute or method is public and can be accessed from outside the class.
Constant State
When class attributes are declared as const, they can only be modified within a constructor (i,e init method). If an attribute is modified outside the constructor, the compiler will raise an error.
In other words, the init function will ignore all const checks for write access on the attributes of the class being initialized.
Implementing Interfaces
Classes in Type-C can implement interfaces, increasing their functionality and making them more versatile.
Classes and Method Overloading
Method overloading is allowed within classes and interfaces. A method signature is what identifies a method, and it is composed of the method name and the types of its parameters. Hence, two methods with the same name but different parameter types are considered overloaded. Return type within the signature is not considered, hence if two methods have the same name and parameter types but different return types, they are considered duplicates and will cause a compilation error.
You can overload an operator by using its symbol or name (surrounded with two underscores) such as + or __add__ It is possible to overload the following operators:
Declaring same Method with Multiple Names
Consider the case where an interface has the following signature:
You now want to have a class that implements this interface, but you want to also use [] to achieve same behavior as get.
separator | is used to separate the different names of the method.
Classes and Inheritance
It is better to think of classes as concrete behaviors rather than a hierarchy of types. Classes implement interface, which are merely protocols for behavior. This is a subtle but important distinction.
Rules
- Class names must start with a capital letter.
- Classes support generics
- Classes can implement multiple interfaces
- Classes can have multiple constructors
- Classes can have static methods
- Classes can have static attributes
- Classes can have implementations
- Classes' static methods cannot use the class generic type
- Classes' attributes cannot be generic
- Classes can extend generic interfaces.
- Classes constructor must return void, they are only meant to initialize the state of the object.
Advanced Topics
Suppose we have the following data types:
This is completely valid, as long as the interface header is compatible with the generic class constraints. Here is a case where is it is not possible:
Will fail since the given type argument does not satisfy the constraints of the generic class.
Also generics that have multiple constraints, will need to have only one when made concrete, otherwise compiler will fail, here is another example: