TYPES
In HookScript, you can declare your own variables and assign values to the them.
There are primitive types and container types
Primitive types
any- see belowint- is used for integers (whole numbers)float- is used for floating point numbers (decimals)text- is used for character strings (letters and words)bool- is used for boolean values (true or false)datetime- (WIP)
Container types
list- a list of valuesdict- a dynamic collection of key-value pairsrecord- a static collection of key-value pairstable- a collection of records
var i int = 0
var b bool = true
var t text = "Hello"
var f float = 1.0
var l list = [1, 2, 3]
var d dict = { name: "pj", age: 7}
var r record<name, age> = ["pj", 7]
var tbl table<name, age> = [ ["PJ", 7], ["Cleo", 6] ]
var a any = 1
any type
The any type is a way to say your variable/container can hold any type. This allows the language to be more
dynamic. Under the hood, HookScript will automatically assign the any type whenever the developer does not explicitly declare a type.
// these two statements are the same
var x = 10
var x any = 10
// these two statements are the same
var list = [1, 2, 3]
var list<any> = [1, 2, 3]
Union types
You can specify union types by separating each type with a pipe symbol |. In the example below, the variable x can hold values of type int or text. You can use union types anywhere
you specify a type, and union type can be made of as many types as you like (e.g., int | text | bool | list<int>)
var x int | text
x = 10
x = "some text"
print(x)
typecasting with as keyword
The as keyword is used to cast from one type to another. This can be useful when assigning values to variables that have declared types.
var x float = (5 + 5) as float
var x = [1, 2, 3] as list<float>
Note: an int type is automatically cast to a float type wherever a float is expected
Named types
HookScript allows you to create your own types, called named types using the type keyword similar to how you would declare a variable. This allows
you to encapsulate logic for us throughout your script. Named types can be used like any other type and comparisons are made by name and by the underlying
type.
type cat = record<name text, age int>
var pj cat = ["PJ", 7]
print(pj["name"])
Named type methods
Named types come with built-in methods
- baseType(): provides the underlying type for the named type as text
- inheritance(): provides the inheritance chain for the named type as a list. The last item in the list will match what is returned by baseType()
Loading Playground