2
Structures
DiamondNether90 edited this page 2025-09-21 16:10:25 +10:00

Structures allow coders to define their own types. They are similar to lists, but they do have a few key differences:

  • Each field of a set can be called at any time
  • You can define functions within the structure, which can be called by using !struct.fun.
  • The fields of a structure are unordered

Let's have a look at how you can create a structure.

The Basics

For our example, we will be making a point structure. This will store a point on the Cartesian plane.

Let's create the framework for the struct:

struct -point

endstruct

All this does is create a new structure, named point.

Defining Fields

A field is, to put it simply, the parts that make up a structure. To define a field, use init &fieldname -type.

For our example, we want to store two variables that store a double: one for the x-position, and one for the y-position. So our fields will be xpos and ypos.

struct -point
    init &xpos -double
    init &ypos -double
    
endstruct

Now we have a complete structure!

Defining a variable as a structure

We have a point, but how can we set a variable as a point? It is pretty simple: just use init &myPoint -point.

To modify the xpos and ypos of the myPoint variable, you can use set &myPoint.xpos $value and set &myPoint.ypos $value.

struct -point
    init &xpos -double
    init &ypos -double
    
endstruct

init &myPoint -point
set &myPoint.xpos 3
set &myPoint.ypos 4

This stores the point (3, 4).

And now you know the basics of making a structure! You know how to define a structure, its fields, and how to define a variable as a structure. But there is more you can do...

Member Functions

One trick you can do is to define functions within the structure. Typically these will be getting some value related to the structure.

For our example, we will find the distance of the point from the origin. Let's first build the framework.

struct -point
    init &xpos -double
    init &ypos -double
    
    fun -double !modulus
        
    endfun
endstruct

The distance from the origin, by the Pythagorean Theorem, is \sqrt{x^2+y^2}. We can simply use $xpos and $ypos to find the respective values when the function is called.

We will import the math function to use the square root function.

struct -point
    init &xpos -double
    init &ypos -double
    
    fun -double !modulus
        multiply $xpos $xpos &x2
        multiply $ypos $ypos &y2
        add $x2 $y2 &z2
        pusharg $z2
        !math:sqrt &z
        return $z
    endfun
endstruct

Now, if we run !myPoint.modulus, we can get the modulus of the vector represented by ($myPoint.xpos, $myPoint.ypos).

And that's all you need to know!

Other examples of structures

  • A user account for a messaging service
struct -user
    init -string &username
    init -string &password
    init -bool &isBanned
    
    fun -bool !init -string &usr -string &pass
        set &username $usr
        set &password $pass
        set &isBanned false
    endfun
    
    fun -bool !banUser
        set &isBanned true
        return true
    endfun
    
    fun -bool !newName -string &user
        set &username $user
        return true
    endfun
endstruct

# Create a user
init &newUser -user
pusharg "Max" "Dingus"
!newUser.init &store

# Change a username
pusharg "Maxwell"
!newUser.newName &store

# Ban a user
!newUser.banUser &store