Member functions
Hybrix classes can be used for object-oriented programming (OOP), a classic software engineering paradigm. It is a relatively deep topic, but the basic idea is simple: you add FUNC members to your CLASS, and then invoke them using the . operator. For example, similar to accessing a DOOR.COLOR member variable, we might call a DOOR.OPEN() member function.
Three different kinds of functions can get inside a CLASS. They are explained below.
"FUNC" members
Let's start with a basic example. The function below has a hidden parameter SELF that allows access to the current instance of the object.
CLASS RECTANGLE
VAR WIDTH: INT
VAR HEIGHT: INT
FUNC GET_AREA(): INT
VAR AREA: INT
SELF.WIDTH * SELF.HEIGHT -> AREA
RETURN AREA
END FUNC
END CLASS
MODULE MAIN
FUNC START()
VAR R: RECTANGLE
NEW RECTANGLE() -> R
# ACCESSING MEMBER VARIABLES
10 -> R.WIDTH
20 -> R.HEIGHT
VAR A: INT
# CALLING A MEMBER FUNC
R.GET_AREA() -> A
END FUNC
END MODULE
Important points:
- The
FUNCmembers must appear after anyVARmembers. For example,VAR WIDTH: INTmust come beforeFUNC GET_AREA(): INT. - The
.operator is used to call a member function likeR.GET_AREA(), just like how we would useR.WIDTHto access a member variable. - Inside that
FUNC, the specialSELFvariable returns the class instance. In our example,SELFrefers to the same object asR. - Instead of writing
SELF.WIDTHORSELF.GET_AREA(), you can simply write.WIDTHor.GET_AREA()for short.
Here's how CLASS RECTANGLE looks when using . instead of SELF.:
# (SAME MEANING AS THE ABOVE DEFINITION)
CLASS RECTANGLE
VAR WIDTH: INT
VAR HEIGHT: INT
FUNC GET_AREA(): INT
VAR AREA: INT
.WIDTH * .HEIGHT -> AREA
RETURN AREA
END FUNC
END CLASS
Constructor functions
"Constructors" are a special member function that is invoked only when an object is being created using NEW. Their purpose is to initialize the new object's member variables.
In the previous section we defined a circle class like this:
CLASS CIRCLE
VAR X: INT, Y: INT
VAR RADIUS: INT
END CLASS
MODULE MAIN
FUNC START()
VAR C1: CIRCLE, C2: CIRCLE
NEW CIRCLE() -> C1
0 -> C1.X
0 -> C1.Y
5 -> C1.RADIUS
NEW CIRCLE() -> C2
10 -> C2.X
20 -> C2.Y
5 -> C2.RADIUS
END FUNC
END MODULE
The statements such as 0 -> C1.X are tedious to write, and it's easy to accidentally forget one of them. Here's an equivalent program that uses a class constructor.
CLASS CIRCLE
VAR X: INT, Y: INT
VAR RADIUS: INT
# THE CLASS CONSTRUCTOR:
FUNC NEW(X: INT, Y: INT, RADIUS: INT)
X -> .X
Y -> .Y
RADIUS -> .RADIUS
END FUNC
END CLASS
MODULE MAIN
FUNC START()
VAR C1: CIRCLE, C2: CIRCLE
# CALL THE CIRCLE.NEW() CONSTRUCTOR FUNC
NEW CIRCLE(0, 0, 5) -> C1
# CALL THE CIRCLE.NEW() CONSTRUCTOR FUNC
NEW CIRCLE(10, 20, 5) -> C2
END FUNC
END MODULE
Important points:
- Class constructors are defined using
FUNC NEW(). In this syntax,NEWis a special keyword. - Class constructors are called automatically by expressions such as
NEW CIRCLE(0, 0, 5) - Constructors are optional.
"HOOK" members
Classes also support another kind of function defined using HOOK instead of FUNC. They behave almost exactly the same as FUNC. The difference has to do with inheritance, which we'll discuss in the next section.