Types
User-defined object types, which can hold special object-local functions and other properties. Useful for interacting with libraries from other JVM languages, like Java and Kotlin.
This is an advanced feature.
Types are used to create objects. ByteSkript has a large number built in (like String, Number, Boolean, etc.) but the type member allows a user to define their own custom type.
All objects have at least one type. The object "hello" is a String. This information is used for syntax like "hello" is a string. Most types allow a new object to be created, such as lists and maps.

Type Structure

Types have a single name input in their member pattern. The type member goes at the root level of the script (with no indent.)
1
type %TypeName%:
2
Copied!
This name is written in standard, a-z0-9_ format, without any quotes or brackets.
1
type Thing:
2
Copied!
For clarity, example type names are written in UpperCamelCase but this is only a style choice to prevent confusion with functions.
A type can hold function members inside it, which need to be indented by an extra unit.
Functions inside the type are considered to belong to that type. These are different from regular functions, but the format for writing them is the same.
1
type Thing:
2
function my_func:
3
trigger:
4
print "hello"
Copied!
A type can have multiple functions inside it. These functions may have parameters.
1
type Thing:
2
function my_func:
3
trigger:
4
print "hello"
5
function second_func (word):
6
trigger:
7
print "hello " + {word}
Copied!
A type can be created and used in another function. This uses the [a] new %Type%
1
type Thing:
2
function my_func:
3
trigger:
4
print "hello"
5
โ€‹
6
function say_hello:
7
trigger:
8
set {thing} to a new Thing // makes a new object
9
assert {thing} exists
10
assert {thing} is a Thing // it's of our type
Copied!
Calling functions that belong to a type works slightly differently.
This uses the function(...) from %Object% syntax.
1
type Thing:
2
function my_func: // this will be run
3
trigger:
4
print "hello"
5
โ€‹
6
function say_hello:
7
trigger:
8
set {thing} to a new Thing
9
assert {thing} exists
10
run my_func() from {thing} // runs my_func
11
// hello was printed
Copied!
Currently, printing the object directly with print {thing} will not look very nice. Special string conversion behaviour can be added using a special toString function.
1
type Thing:
2
function toString: // this is a special function
3
return: String // this is necessary here
4
trigger:
5
return "My Cool Thing"
6
โ€‹
7
function say_hello:
8
trigger:
9
set {thing} to a new Thing
10
assert {thing} exists
11
print {thing} // this will run our toString function internally
Copied!
The toString function is overriding the built-in Object type's toString method, and allowing the user to control what it prints.
The return: String is necessary to make sure the compiler knows exactly which function to override.
As type functions are local to that particular type, two separate custom types may define identical functions without issue.
1
type Thing:
2
function hello:
3
trigger:
4
print "hello"
5
โ€‹
6
type Box:
7
function hello:
8
trigger:
9
print "goodbye"
Copied!

Templates

Types may use multiple template types.
1
type Square:
2
template: Shape
3
template: Thing
4
function hello:
5
trigger:
6
print "hello"
Copied!
An explanation of how these work can be found here.

Properties

Types may declare properties. These are value-storing members of the type, that function a bit like variables attached to each object.
Unlike variables, an individual property exists for each object of the custom type.
1
type Shape:
2
property name:
3
property size:
Copied!
These properties can be used and set with the <property> of %Object% syntax.
1
set {thing} to a new Shape
2
set name of {thing} to "Square"
3
set size of {thing} to 100
4
assert name of {thing} is "Square"
5
assert size of {thing} is 100
Copied!
You may specify the type of these properties, so that they will only accept a certain type of object.
1
type Shape:
2
property name:
3
type: String
4
property size:
5
type: Number
Copied!
If the property is set to the wrong type, an error will appear. This can be used to prevent other scripts from wrongly-using the type.
1
set {thing} to a new Shape
2
set name of {thing} to "Square" // fine
3
set name of {thing} to 42 // error
Copied!
The properties of two different objects of the same type are separate. They can have different values.
1
set {a} to a new Shape
2
set {b} to a new Shape
3
assert {b} is not {a}
4
set name of {a} to "Square"
5
set name of {b} to "Circle"
6
assert name of {a} is "Square"
7
assert name of {b} is "Circle"
Copied!
Two different types can have a property with the same name.
1
type Person:
2
property name:
3
type: String
4
โ€‹
5
type Car:
6
property name:
7
type: String
Copied!
A type can have both functions and properties.
1
type Person:
2
property name:
3
type: String
4
function say_hello:
5
trigger:
6
print "hello " + this object + "!"
Copied!