Skip to main content

Class operators

There are a couple operators for working with classes.

Type cast with "AS"

The AS operator is used with objects that are instances of a class. For example, A AS B can be used to test whether A is an instance of the base class B.

In order to use this operator, the class must extend from the special CLASS_ID_BASE base class.

Suppose we define classes CAT and DOG that both extend from an ANIMAL base class:

CLASS ANIMAL EXTENDS CLASS_ID_BASE
VAR NAME: STRING
END CLASS

CLASS CAT EXTENDS ANIMAL
FUNC MEOW()
CONSOLE::PRINT("MEOW")
END FUNC
END CLASS

CLASS DOG EXTENDS ANIMAL
FUNC BARK()
CONSOLE::PRINT("BARK")
END FUNC
END CLASS

Objects created with NEW CAT() or NEW DOG() can be assigned to a variable of type ANIMAL. But the ANIMAL type cannot "see" specialized members such as MEOW() or BARK(). In order to access those members, we must "cast" to the type.

MODULE MAIN
FUNC TALK(A: ANIMAL)
VAR C: CAT, D: DOG

# SEE IF WE CAN CAST "A" AS A CAT
A AS CAT -> C

# IF "A" IS NOT A CAT, THEN "C" WILL BE NULL
IF C <> NULL THEN
C.MEOW()
ELSE
# SEE IF WE CAN CAST "A" AS A DOG
A AS DOG -> D

# IF "A" IS NOT A DOG, THEN "D" WILL BE NULL
IF D <> NULL THEN
D.BARK()
END IF
END IF
END FUNC

FUNC START()
CONSOLE::INIT()
MAIN::TALK(NEW CAT())
MAIN::TALK(NEW DOG())
END FUNC
END MODULE

Note: To cast types that are not classes, use the conversion operators such as TO_BYTE() instead of AS.

CLASS_ID() operator

This operator returns an INT value that uniquely identifies a Hybrix class, useful for detecting the type of an object. For example, CLASS_ID(C) can be used to obtain an INT that uniquely identifies the class or object C.

The class must extend from the special CLASS_ID_BASE base class.

Example:

CLASS THING EXTENDS CLASS_ID_BASE
FUNC MEMBER()
VAR ID: INT

# YOU CAN ASK FOR THE CLASS_ID OF AN OBJECT
# (CALCULATED AT RUNTIME)
CLASS_ID(SELF) -> ID
END FUNC
END CLASS

MODULE MAIN
FUNC START()
VAR ID: INT

# YOU CAN ALSO ASK FOR THE CLASS_ID OF A CLASS TYPE
# (A CONSTANT VALUE KNOWN AT COMPILE TIME)
CLASS_ID(THING) -> ID
END FUNC
END MODULE