TechScript provides support for class-based object-oriented programming to structure state and behaviors.
Use the class keyword to define a class. Methods are declared inside using the do keyword. The constructor must be named init:
class Person
# Properties with default values
name = ""
age = 0
# Constructor
do init(name, age)
self.name = name
self.age = age
end
# Instance method
do greet()
say $"Hello, my name is {self.name}."
end
end
Classes are instantiated using the new keyword:
# Instantiate class object
bob = new Person("Bob", 25)
# Call instance method
bob.greet() # Hello, my name is Bob.
By default, all fields and methods on a class are public. Properties can be accessed and modified directly:
bob.age = 26
say bob.age # 26
To implement behavior-only structures, see Interfaces and Traits.