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.)
This name is written in standard, a-z0-9_
format, without any quotes or brackets.
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.
A type can have multiple functions inside it. These functions may have parameters.
A type can be created and used in another function. This uses the [a] new %Type%
Calling functions that belong to a type works slightly differently.
This uses the function(...) from %Object%
syntax.
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.
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.
Templates
Types may use multiple template types.
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.
These properties can be used and set with the <property> of %Object%
syntax.
You may specify the type of these properties, so that they will only accept a certain type of object.
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.
The properties of two different objects of the same type are separate. They can have different values.
Two different types can have a property with the same name.
A type can have both functions and properties.
Last updated