Tekst
Source
Documentation/Classes & Objectssource tree
Learn

Classes & Objects

Tekst supports classes with constructors, methods, object fields, and inheritance.

Define a class

counter.tk
class Counter:
    fn __init__(self, start=0):
        self.value = start

    fn increment(self, amount=1):
        self.value += amount
        return self.value

counter = Counter(10)
print(counter.increment())

Fields and attributes

Object fields are stored through self. Attribute access uses dot notation.

fields.tk
class User:
    fn __init__(self, name):
        self.name = name

user = User("Ayaan")
print(user.name)

Inheritance

Classes can inherit from another class with extends.

inheritance.tk
class Animal:
    fn speak(self):
        print("...")

class Dog extends Animal:
    fn speak(self):
        print("woof")

Constructors

A method named __init__ is used as the constructor.

constructor.tk
class Point:
    fn __init__(self, x, y):
        self.x = x
        self.y = y

p = Point(10, 20)
Classes, inheritance with extends, constructors, methods, self, fields, and attribute access are part of the current native compiler feature set.